Getting a warning message mysqli_query expects parameter 1 to be mysqli, null given

妖精的绣舞 提交于 2021-01-20 12:17:05

问题


I keep getting this error from my code

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

Here is my code:

$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));

What am I doing wrong?


回答1:


You didn't call mysqli_connect first, or it failed. Set $connection to a valid connection first:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = mysqli_connect("hostname", "username", "password");
$result = mysqli_query($connection, ...);



回答2:


The function mysqli_connection fails (or did you forget calling it?) and returns null which causes the error when you try to use $connection later on with mysqli_query.



来源:https://stackoverflow.com/questions/24859392/getting-a-warning-message-mysqli-query-expects-parameter-1-to-be-mysqli-null-gi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!