【问题标题】:PHP Database connection file not working globally [duplicate]PHP数据库连接文件无法全局工作[重复]
【发布时间】:2016-10-20 04:38:50
【问题描述】:

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

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

致命错误:未捕获的错误:在 C:_server\htdocs\sites_\tfs\core\php\populate.php:5 中调用 null 上的成员函数 query() 堆栈跟踪:#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');
?>

【问题讨论】:

  • 你在哪里包含了 include_once 文件?

标签: php mysql mysqli scope


【解决方案1】:

在您的populate.php 中,在function tableEnlisted() { 的开头或文件的开头添加global $conn;

【讨论】:

    猜你喜欢
    • 2016-07-30
    • 2017-07-02
    • 1970-01-01
    • 2013-07-04
    • 2018-10-22
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多