获取警告消息mysqli_query期望参数1为mysqli,null给定

时间:2014-07-21 07:07:15

标签: php mysqli

我不断从我的代码

中收到此错误
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\citizenssuites\site\pages\reservations.php on line 26

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\citizenssuites\site\pages\reservations.php on line 26
Error querying database the first time

这是我的代码:

$query = "SELECT * FROM reservations, rooms, room_type";
$query .= " WHERE reservations.room_id=rooms.room_id";
$query .= " AND rooms.room_type_id=room_type.room_type_id";
$query .= " AND reservations.arrival < ".$arrival;
$query .= " AND reservations.departure > ".$departure;
$query .= " ORDER BY room_type.room_type_id";
$result = mysqli_query ($connection, $query) or die ('Error querying database the first time' . mysqli_error($connection));

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您没有先调用mysqli_connect,否则失败。首先将$connection设置为有效连接:

$connection = mysqli_connect("hostname", "username", "password");
if (mysqli_connect_error()) {
    die("Error: " . mysqli_connect_error());
}
$result = mysqli_query($connection, ...);

答案 1 :(得分:0)

函数mysqli_connection失败(或者您忘记调用它?)并返回null,当您稍后尝试$connection使用mysqli_query时会返回错误。