尝试使用此函数从我的数据库中获取未读消息,但我以这样的资源ID错误结束:“Resource id #45
”
function unread_message_count() {
$sql = "SELECT conversations_messages.message_id, conversations_members.user_id, conversations_members.conversation_last_view, conversations_messages.message_date
FROM conversations_members
INNER JOIN conversations_messages ON conversations_messages.user_id = conversations_members.user_id
WHERE conversations_members.conversation_last_view < conversations_messages.message_date AND conversations_members.user_id = {$_SESSION['user_id']}";
$result = mysql_query($sql);
return $result;
}
我以这种方式调用函数,但没有打印出来:
$count = unread_message_count();
echo $count;
答案 0 :(得分:0)
mysql_query()会在成功时返回资源,或者在出现错误时FALSE
,您需要从结果中获取数据,例如:
...
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);
return $rows;
答案 1 :(得分:0)
mysql_query()
返回资源。 to string (通过使用echo
隐式触发输出)资源ID#后跟id。
PHP中的资源只能与其他PHP函数一起使用。这包括但不限于文件,curl,ftp句柄等。
使用mysql_fetch_array()
(或类似)
试试这个
function unread_message_count() {
$sql = "SELECT conversations_messages.message_id, conversations_members.user_id, conversations_members.conversation_last_view, conversations_messages.message_date
FROM conversations_members
INNER JOIN conversations_messages ON conversations_messages.user_id = conversations_members.user_id
WHERE conversations_members.conversation_last_view < conversations_messages.message_date AND conversations_members.user_id = {$_SESSION['user_id']}";
$result = mysql_query($sql);
$data = mysql_num_rows($result);
return $data;
}