我收到此警告:
( ! ) 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() 函数时,我得到了警告。
数据库:
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>