使用另一个PHP文件中包含的变量

时间:2015-03-31 17:12:14

标签: php mysql scope

所以我有一个config.php文件,我想拥有登录凭据。

的config.php

<?php
  $server = "localhost";
  $username = "root";
  $password = "admin123";
  $database = "web_apps";
  $table = "agencies";
?>

我有一个connection.php文件,其中包含config.php文件。

connection.php

<?php
require_once('config.php');
$connection = new mysqli($server, $username, $password, $database);
if(mysqli_connect_errno()){
    echo "Connection could not be established";
    exit();
}
$url_id = isset($_GET["id"])?$_GET["id"]:NULL;
$query = 'SELECT * FROM '.$table;
$stmt = $connection->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$all_data =array();
$specific = array();
$name_list = array();
$fieldname = array();
$datatype = array();
$searchable = array();
$search_keys = array();
$name_keys = array();
$i = 0;
$m = 0;
while ($row = $result->fetch_array(MYSQLI_NUM))
{
    if($i == 0){
        $fieldname = $row;
    }
    else if($i == 1){
        $datatype = $row;
    }
    else{
        $all_data[$row[0]]= $row;
        $name_keys[$m++] = $row[0];
    }
    $i++;
}
foreach ($all_data as $each_service){
    if(!strcmp($url_id, $each_service[0])){
        $specific = $each_service;
       break;
    }
}
for($i = 0; $i < count($datatype); $i++){
    if(strpos($datatype[$i], "search_")!== FALSE){
        $searchable[$i] = $datatype[$i];
        $search_keys[$i] = $i;
    }
}
$name_index = 0;
foreach ($datatype as $key) {
    if(strpos($key,"name") !== false){
        break;
    }
    $name_index++;
}

foreach ($all_data as $key=>$value) {
    $name_list[$key] = $value[$name_index];
}
//echo $name_index;
//print_r($names);
//var_dump($all_data);
//print_r($name_list);
//print_r($specific);
//print_r($fieldname);
//print_r($datatype);
//print_r($searchable);
//print_r($search_keys);
//print_r($name_keys);
$connection->close();
?>

connection.php文件应该使用config.php中的配置变量并建立与数据库的连接并将数据丢弃到其他文件。这两个文件都在同一目录中。我有另一个文件index.php requires_once connection.php并在应用程序中显示数据。

的index.php

<?php require_once("core/connection.php"); ?>
<?php require_once("core/header.php"); ?>

    <div data-role="page" data-theme="a">
        <div data-role="main" class="ui-content main-content">
            <?php require_once('core/topbar.php'); ?>

            <select id="searchby">
                <option value="" selected disabled>Search by ... </option>
                <?php
                foreach($search_keys as $key){
                      echo "<option value=".$key.">".$fieldname[$key]."</option>";                    
                  }
                ?>
            </select>
            <form class="ui-filterable">
                <input id="autocomplete-input" data-type="search" placeholder=<?php echo '"Search by '.$fieldname[$search_category].'"'; ?>>
            </form>
            <ul data-role="listview" data-filter="true" data-filter-reveal="true" data-input="#autocomplete-input" data-inset="true">
                        <?php
                        if($search_category!=NULL){
                            $i = 0;
                            foreach ($all_data as $names){
                                echo "<li id='".$name_keys[$i]."'><a href='info.php?id=".$name_keys[$i++]."'>".$names[$search_category]." Name =  ".$names[$name_index]."</a></li>";
                            }
                        }
                        ?>
            </ul>
        </div>
        <div style="text-align:center">
            <img width="90%" src="img/cdcs-home.png" />
        </div>
<?php require_once("core/footer.php"); ?>

问题

connection.php将显示从数据库中提取的var_dump数据。但是index.php不会。 每当我打开index.php时,都会说:

Notice: Undefined variable: server in C:\xampp\htdocs\cdcs\core\connection.php on line 4

Notice: Undefined variable: username in C:\xampp\htdocs\cdcs\core\connection.php on line 4

Notice: Undefined variable: password in C:\xampp\htdocs\cdcs\core\connection.php on line 4

Notice: Undefined variable: database in C:\xampp\htdocs\cdcs\core\connection.php on line 4

Notice: Undefined variable: table in C:\xampp\htdocs\cdcs\core\connection.php on line 10

Fatal error: Call to a member function execute() on boolean in C:\xampp\htdocs\cdcs\core\connection.php on line 12

如果我声明并初始化connection.php中的所有配置变量,index.php会显示应用程序中的数据。

我希望原样拥有3个文件,并且仍会通过index.php

显示config.php中配置的数据库中的connection.php信息

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:4)

您正在执行index.php中的C:\xampp\htdocs\cdcs\

您正在执行require_once 'core/connection.php'包含将脚本内容编入您的index.php。因此,文件connection.php中的任何代码现在都在index.php所在的同一目录中运行。

因此无法找到config.php并且无法包含,因此您的变量未定义。该脚本会查找C:\xampp\htdocs\cdcs\config.php而不是C:\xampp\htdocs\cdcs\core\config.php

要解决此问题,请在require_once 'core/config.php'内使用connection.php或在php.ini中正确使用include-dir

Sidenode:您应该使用Constants而不是variables,因为变量用于更改内容,而您的数据库凭据在执行期间通常不会这样做。