sql + php - 致命错误:不能使用mysqli_result类型的对象作为数组的原因

时间:2013-08-05 13:46:17

标签: php sql

这是我的代码:

<?php

$con = mysqli_connect("127.0.0.1:3306","root","root","photoshare");
$query = "SELECT ID,nickname,photoLikes FROM tbl_photo";
$result = mysqli_query($con,$query);
$query_dislike = "SELECT nickname, idGivenLike FROM tbl_check_like";
$resultDislike = mysqli_query($con,$query_dislike);
$photoIDdislike;
$photoID;
while($photoID = mysqli_fetch_assoc ($result) || $photoIDdislike = mysqli_fetch_assoc($resultDislike)   )
{
//checks if both of results
if($result["ID"] != $resultDislike["idGivenLike"])
{
echo "true";
}
}
?>

当我运行它时会说下一行

if($result["ID"] != $resultDislike["idGivenLike"])

h致命错误:Fatal error: Cannot use object of type mysqli_result as array

为什么它不起作用,我怎么能解决它?

2 个答案:

答案 0 :(得分:3)

if($result["ID"] != $resultDislike["idGivenLike"])

应该是:

if($photoID["ID"] != $photoIDdislike["idGivenLike"])

$resultDislike/$result是您的MySQL资源,$photoID/$photoIDdislike是获取的行(数组)。

答案 1 :(得分:0)

应该是

$photoID["ID"] != $photoIDdislike["idGivenLike"]
// In script 
if($photoID["ID"] != $photoIDdislike["idGivenLike"])
{
   echo "true";
}