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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?