php – mysqli_query()警告参数1为mysqli,给定null.数据库查询失败

我收到这个警告:

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\inc\functions.php on line 73
Call Stack
#   Time    Memory  Function    Location
1   0.0003  251048  {main}( )   ..\manage_user.php:0
2   0.0012  295000  find_all_users( )   ..\manage_user.php:8
3   0.0012  295376  mysqli_query ( )    ..\functions.php:73
Database query failed.

我已经阅读了一些关于它的帖子以及我在mysqli_query($db,$query)中的第一个字符串中的错误.

这将是$db这是我觉得奇怪的. mysqli_query($db,$query)适用于所有其他函数.但是当我调用find_all_users()函数时,我会收到警告.

dbcon:

    define("DB_SERVER", "localhost");
    define("DB_USER", "*******");
    define("DB_PASS", "*******");
    define("DB_NAME", "*******");

  // 1. Create a database connection
  $db = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
  // Test if connection succeeded
  if(mysqli_connect_errno()) {
    die("Database connection failed: " . 
         mysqli_connect_error() . 
         " (" . mysqli_connect_errno() . ")"
    );
  }

功能:

function find_all_users() {
    global $db;
$query  = "SELECT * ";
$query .= "FROM user ";
$query .= "ORDER BY username ASC";
$user_set = mysqli_query($db, $query);
    confirm_query($user_set);
    return $user_set;
}

function confirm_query($result_set) {
    if (!$result_set) {
        die("Database query failed.");
    }
}

我收到警告的页面:

<?php 
    require_once($_SERVER['DOCUMENT_ROOT'].'/session/session.php'); 
    require_once($_SERVER['DOCUMENT_ROOT'].'/inc/functions.php');
    include($_SERVER['DOCUMENT_ROOT'].'/inc/header.php');
    include($_SERVER['DOCUMENT_ROOT'].'/inc/nav_ribbon.php');
    $user_set = find_all_users();
?>

<body>
    <div id="p2dbg">
    <div id="p2dcontent">
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/left_container.php'); ?>
      <div id="page">
        <?php echo message(); ?>
      </div>
    </div>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php'); ?>
    </div>
</body>
</html>

最佳答案 $db为null,因为未包含dbcon文件:

include('dbcon.php');
session_start();
点赞