当我运行代码时,它会显示此错误。 “mysqli_query()期望参数1为mysqli,null为”
<?php
function confirm_query($result_set) {
if (!$reslt_set) {
die("database query failed");
}
}
function find_all_subjects() {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible=1 ";
$query .= "ORDER BY position ASC ";
$subject_set = mysqli_query($connection, $query);
confirm_query($subject_set);
return $subject_set;
}
?>
答案 0 :(得分:1)
如果这是您的完整文件,那么$connection
将为空(null),您不会在任何地方创建连接。
您可能正在另一个文件中创建连接,因此global $connection;
但是您需要包含此文件才能获得连接。
答案 1 :(得分:0)
您正在混合mysql_
和mysqli
个功能。这可能是你的问题。你不能在代码中混合使用mysqli和mysql函数。
答案 2 :(得分:0)
试试这个。
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Create table doesn't return a resultset */
if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
printf("Table myCity successfully created.\n");
}
/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* free result set */
mysqli_free_result($result);
}
仅限php.net