PHP数据库连接文件无法全局运行

时间:2016-10-20 04:38:50

标签: php mysql mysqli scope

在普通的PHP文件中,包含 的文件包含它,$conn变量全球化:global $conn;,PHP会返回此错误。

  

注意:未定义的变量:第5行的C:_server \ htdocs \ sites_ \ tfs \ core \ php \ populate.php中的conn

     

致命错误:未捕获错误:在C中调用null上的成员函数query():_ server \ htdocs \ sites_ \ tfs \ core \ php \ populate.php:5堆栈跟踪:#0 C:_server \ htdocs \ sites_ \ tfs \ admin \ index.php(59):tableEnlisted()#1 {main}在第5行的C:_server \ htdocs \ sites_ \ tfs \ core \ php \ populate.php中抛出

db.php中

<?php

 global $conn;
  $u = 'username';
  $p = 'password';
  $h = 'example.net';
  $db = 'example';

  // Create connection
  $conn = new mysqli($h, $u, $p, $db);
  // Check connection
  if ($conn->connect_error) {
   msg('error',"There was a fatal error connecting to the database: ".    $conn->connect_error, 'Fatal Database Error');
  }

populate.php(目前唯一请求$conn的核心文件)

<?php

  function tableEnlisted() {
    $sql = "SELECT A_I, qid, toy, toy_varient, date FROM tfs_enlisted ORDER BY A_I DESC LIMIT 500";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {
        $sql = "SELECT last_name, firstname, class FROM tfs_students WHERE qid LIKE `".$row['qid']."`";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
          while($row = $result->fetch_assoc()) {
              $last = $row['last_name'];
              $first = $row['last_name'];
              $class = $row['class'];
              echo
              '<tr>
                <td>'.$row['A_I'].'</td>
                <td>'.$last.'</td>
                <td>'.$first.'</td>
                <td>'.$class.'</td>
                <td>'.$row['toy'].'</td>
                <td>'.$row['toy_varient'].'</td>
                <td>'.$row['date'].'</td>
                <td>
                  <div class="btn-group">
                    <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">
                      Options
                    </button>
                    <div class="dropdown-menu">
                      <a href="view?mode=edit&amp;row='.$row['A_I'].'"><span class="text-primary">Edit</span></a>
                      <a href="view?mode=view&amp;row='.$row['A_I'].'"><span class="text-primary">View</span></a>
                      <a href="view?mode=void&amp;row='.$row['A_I'].'"><span class="text-danger">Void</span></a>
                    </div>
                  </div>
                </td>
              </tr>';
          }
        }
        else {
          msg('warn','User with the ID '.$row['qid'].' does not exist!','Unknown User');
        }
      }
    }
    else {
      msg('info','The table returned empty.','Empty!');
    }
  }

include_once订单

<?php
  include_once('../core/php/msgs.php');
  include_once('../core/php/db.php');
  include_once('../core/php/oauth.php');
  include_once('../core/php/populate.php');
?>

1 个答案:

答案 0 :(得分:-1)

populate.php开头global $conn;或文件开头function tableEnlisted() {添加bash