简体   繁体   English

MySQL与PHP的联接查询

[英]mysql join query with php

table: database1.comment
id | owner_id | comment    
1  |  1       | some words 
2  |  1       | some words
3  |  2       | some words

table: database2.users
id | display_name
1  | admin    
2  | guest

I am try to join 2 tables query, here is my php code: 我尝试加入2个表格查询,这是我的php代码:

$result = mysql_query("SELECT * FROM database1.comment INNER JOIN database2.users ON database1.comment.owner_id=database2.users.id order by database1.comment.id DESC");
while ($row = mysql_fetch_array($result)){
     echo '<li>'.$row['display_name'].': '.$row['comment'].'</li>';
}

I get a error message: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given . 我收到一条错误消息: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given How to make a work code ( optimize way ) 如何编写工作代码(优化方式)

If your query is invalid (or otherwise "failed"), mysql_query() returns "false" (instead of an array). 如果您的查询无效(或否则为“失败”),则mysql_query()返回“ false”(而不是数组)。

SUGGESTIONS: 建议:

  1. Cut/paste your SQL string into MySQL, see what's incorrect, and fix it. 将您的SQL字符串剪切/粘贴到MySQL中,查看不正确的地方并进行修复。 And that point, your PHP should be OK. 至此,您的PHP应该可以了。

  2. Check your result (and gracefully handle any failures/errors) in your code: 在代码中检查结果(并妥善处理所有失败/错误):

EXAMPLE (error: no "from" clause): 示例(错误:没有“ from”子句):

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

mysql_query return resource on success and boolen false on failure when there is no results. 如果没有结果,则mysql_query成功返回资源,失败则返回boolen false。

http://php.net/manual/en/function.mysql-query.php http://php.net/manual/zh/function.mysql-query.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM