I have the following code:
我有以下代码:
function fetch_conversation_messages($conversation_id){
$conversation_id = (int)$conversation_id;
$sql = "SELECT
`conversations_messages`.`message_date`,
`conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
`conversations_messages`.`message_text`,
`users`.`user_name`
FROM `conversations_messages`
INNER JOIN `users` ON `conversations_messages`.`user_id` = `users`.`user_id`
INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
AND `conversations_members`.`user_id` = {$_SESSION['user_id']}
ORDER BY `conversations_messages`.`message_date` DESC";
$result = mysql_query($sql);
var_dump($result);
$messages = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$messages[] = array(
'date' => $row['message_date'],
'unread' => $row['message_unread'],
'text' => $row['message_text'],
'user_name' => $row['user_name'],
);
}
var_dump( $messages );
}
}
It should return something like this:
它应该返回这样的东西:
Array ( [0] => Array ( [date] => 1322254667 [text] => one [user_name] => bob ) )
However, it returns
然而,它返回
resource(16) of type (mysql result) array(0) { }
I am completely new to PHP and MySQL, but I have tried checking for typos, echoing mysql_error, and killing the script at an error, which shows that there is no error in the SQL.
我是PHP和MySQL的新手,但我已经尝试检查拼写错误,回显mysql_error,并在出错时终止脚本,这表明SQL中没有错误。
I do not know what has gone wrong and how to fix it.
我不知道出了什么问题以及如何解决它。
Please help me. Thanks in advance.
请帮帮我。提前致谢。
3 个解决方案
#1
1
var_dump($result);
shows resource(16) of type (mysql result)
means your query is ok.
后续代码var_dump($结果);显示类型的资源(16)(mysql结果)表示您的查询正常。
var_dump($messages);
shows empty array means the result of your query is empty.
的var_dump($消息);显示空数组表示查询结果为空。
#2
0
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query()
returns a resource on success, or FALSE on error.
对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()在成功时返回资源,如果出错则返回FALSE。
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query()
returns TRUE on success or FALSE on error.
对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP等,mysql_query()在成功时返回TRUE,在出错时返回FALSE。
#3
0
mysql_query
function is expected to return resource
when your query is valid.
当查询有效时,mysql_query函数应返回资源。
For you to see the actual returned result of your query, use row fetching functions like mysql_fetch_assoc
, mysql_fetch_object
, and the like. In your case, you put the fetched values in your $row variable, so you may use var_dump on it.
为了查看查询的实际返回结果,请使用行提取函数,如mysql_fetch_assoc,mysql_fetch_object等。在您的情况下,您将获取的值放在$ row变量中,因此您可以在其上使用var_dump。
while ($row = mysql_fetch_assoc($result)){
var_dump($row);
}
Try to check this reference.
尝试检查此参考。
#1
1
var_dump($result);
shows resource(16) of type (mysql result)
means your query is ok.
后续代码var_dump($结果);显示类型的资源(16)(mysql结果)表示您的查询正常。
var_dump($messages);
shows empty array means the result of your query is empty.
的var_dump($消息);显示空数组表示查询结果为空。
#2
0
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query()
returns a resource on success, or FALSE on error.
对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()在成功时返回资源,如果出错则返回FALSE。
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query()
returns TRUE on success or FALSE on error.
对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP等,mysql_query()在成功时返回TRUE,在出错时返回FALSE。
#3
0
mysql_query
function is expected to return resource
when your query is valid.
当查询有效时,mysql_query函数应返回资源。
For you to see the actual returned result of your query, use row fetching functions like mysql_fetch_assoc
, mysql_fetch_object
, and the like. In your case, you put the fetched values in your $row variable, so you may use var_dump on it.
为了查看查询的实际返回结果,请使用行提取函数,如mysql_fetch_assoc,mysql_fetch_object等。在您的情况下,您将获取的值放在$ row变量中,因此您可以在其上使用var_dump。
while ($row = mysql_fetch_assoc($result)){
var_dump($row);
}
Try to check this reference.
尝试检查此参考。