douluxia0606 2015-10-12 13:17
浏览 19
已采纳

如何从config.php文件中读取值到另一个文件[关闭]

I am new to coding with php and I need help with this.

I have a config.php file with these values

<?php
 define('DATABASE_HOST', 'localhost');
 define('DATABASE_NAME', 'voltrun');
 define('DATABASE_USERNAME', 'joy');
 define('DATABASE_PASSWORD', 'salty');
 ?>

I have also a db.php file with these values

<?php require_once"../config.php";
 echo 'DATABASE_HOST';
 echo 'DATABASE_NAME';
 echo 'DATABASE_USERNAME';
 echo 'DATABASE_PASSWORD'

  $db_host = 'DATABASE_HOST';
  $db_user = 'DATABASE_USERNAME';
  $db_password =  'DATABASE_PASSWORD';
  $db_database = 'DATABASE_NAME';

   $connection = mysqli_connect($db_host, $db_user, $db_password,    $db_database);

      if (!$connection) {
die('QUERY FAILED' . mysqli_error($connection));
   } 


   ?>

Any help on how to get the parameters from the config.php file into the db.php variables? As I still get Query Failed error.

Thanks for the help.

  • 写回答

3条回答 默认 最新

  • dongyuan1902 2015-10-12 13:24
    关注

    This part of your code:

     echo 'DATABASE_HOST';
     echo 'DATABASE_NAME';
     echo 'DATABASE_USERNAME';
     echo 'DATABASE_PASSWORD'
                             ^ missing ; if that's your actual code.
    
      $db_host = 'DATABASE_HOST';
      $db_user = 'DATABASE_USERNAME';
      $db_password =  'DATABASE_PASSWORD';
      $db_database = 'DATABASE_NAME';
    

    You've wrapped all your constants in quotes, treating them as string literals; remove them.

    Plus, you've a missing semi-colon:

     echo DATABASE_HOST;
     echo DATABASE_NAME;
     echo DATABASE_USERNAME;
     echo DATABASE_PASSWORD; // < Added the missing semi-colon
    

    that error reporting would have thrown a parse error

      $db_host = DATABASE_HOST;
      $db_user = DATABASE_USERNAME;
      $db_password =  DATABASE_PASSWORD;
      $db_database = DATABASE_NAME;
    

    References:

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // rest of your code
    

    Sidenote: Displaying errors should only be done in staging, and never production.

    Also make sure that this is correct:

    require_once"../config.php";
    

    Error reporting will tell you that also.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧