gpt4 book ai didi

php - mysql_query() 期望参数 2 是资源, bool 值给出

转载 作者:行者123 更新时间:2023-11-29 14:05:42 24 4
gpt4 key购买 nike

我查看了与我类似的问题的其他答案,但似乎无法解决这个问题。

这是代码。

  $connection = mysql_connect("localhost","root","starwars");
$conn = mysql_select_db("project", $connection);

// This code assumes $itemID is set to that of
// the item that was just rated.
// Get all of the user's rating pairs
$sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue
as rating_difference
FROM rating r, rating r2
WHERE r.userID=$userID AND
r2.itemID=$itemID AND
r2.userID=$userID;";

$db_result = mysql_query($sql, $conn);
echo "The result is {$db_result}";
$num_rows = mysql_num_rows($db_result)or die('Cannot Execute:'. mysql_error());

显示的错误是:

Warning: mysql_query() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\recomender\ratingfiles\class.rating.php on line 177

第 177 行是

   $db_result = mysql_query($sql, $conn);

如果我回显 $conn它给出了“1”的值,我认为它等于 true,因此是 bool 值,有什么想法吗?

最佳答案

传递 $connection 作为第二个参数,而不是 $conn

mysql_select_db的结果赋值给$conn,mysql_select_db返回true或false,而不是连接资源。

关于php - mysql_query() 期望参数 2 是资源, bool 值给出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14396996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com