为什么这个 MySQL 查询可以在 phpmyadmin 中工作,但在我的 php 脚本中不返回任何内容?
我在 phpmyadmin 中尝试了以下查询,它返回了正确的结果,但是当我尝试将结果传递给 php 中的变量时,该变量基本上是空的(即,当我尝试在 while 循环中使用 mysql_fetch_array 时,我什么也得不到并且 mysql_num_rows 仅返回 1)。
这是否与我在 MySQL 中创建临时表有关?这是查询:
CREATE TEMPORARY TABLE solved
SELECT comments.nid FROM flag_content
LEFT JOIN comments ON flag_content.content_id=comments.cid
LEFT JOIN term_node ON term_node.nid=comments.nid
WHERE flag_content.fid=3 AND term_node.tid=522;
SELECT node.nid, node.title, users.name, node_counter.totalcount, node_comment_statistics.comment_count, node_comment_statistics.last_comment_timestamp
FROM node
LEFT JOIN term_node ON node.nid = term_node.nid
LEFT JOIN node_comment_statistics ON node.nid = node_comment_statistics.nid
LEFT JOIN node_counter ON node.nid = node_counter.nid
LEFT JOIN users ON node.uid = users.uid
LEFT JOIN solved ON node.nid=solved.nid
WHERE term_node.tid=522 AND solved.nid IS NULL;
我将此查询存储在 PHP 函数中:
function getPosts(){
dbConnect(); //establishes connection
//"....." in the following line is the above query
return mysql_query(".......") or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
}
调用它的代码行是:
$result = getPosts();
当我调用 getPosts() 时,查询因致命错误而终止。我收到同一错误的两个版本,具体取决于查询是否使用“.”跨多行连接。或者简单地写在一行上。
跨多行连接时出错:
Error: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'comments.nid FROM flag_content LEFT JOIN comments ON flag_content.content_id=com' at line 1
在单行上写入时出错:
Error: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT node.nid, node.title, users.name, node_counter.totalcount, node_comment_s' at line 1
I have tried the following query in phpmyadmin and it returns the correct results, but when I try passing the result to a variable in php that variable is essentially empty (i.e., when I try using it in a while loop with mysql_fetch_array, I get nothing. And mysql_num_rows returns only 1).
Could this be tied to the fact that I'm creating a temporary table in MySQL? Here's the query:
CREATE TEMPORARY TABLE solved
SELECT comments.nid FROM flag_content
LEFT JOIN comments ON flag_content.content_id=comments.cid
LEFT JOIN term_node ON term_node.nid=comments.nid
WHERE flag_content.fid=3 AND term_node.tid=522;
SELECT node.nid, node.title, users.name, node_counter.totalcount, node_comment_statistics.comment_count, node_comment_statistics.last_comment_timestamp
FROM node
LEFT JOIN term_node ON node.nid = term_node.nid
LEFT JOIN node_comment_statistics ON node.nid = node_comment_statistics.nid
LEFT JOIN node_counter ON node.nid = node_counter.nid
LEFT JOIN users ON node.uid = users.uid
LEFT JOIN solved ON node.nid=solved.nid
WHERE term_node.tid=522 AND solved.nid IS NULL;
I have this query stored in a PHP function:
function getPosts(){
dbConnect(); //establishes connection
//"....." in the following line is the above query
return mysql_query(".......") or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
}
And the line of code that calls it is:
$result = getPosts();
When I call getPosts(), the query dies with a fatal error. I get two versions of the same error depending on whether the query is concatenated across multiple lines with "." or simply written on a single line.
Error when concatenated across multiple lines:
Error: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'comments.nid FROM flag_content LEFT JOIN comments ON flag_content.content_id=com' at line 1
Error when written on a single line:
Error: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT node.nid, node.title, users.name, node_counter.totalcount, node_comment_s' at line 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一次执行一个查询。创建临时表,选择其中,然后将其作为三个单独的查询删除。作为一项安全预防措施,sql 套接字只允许在执行块中执行一个查询,
它看起来像......
try executing the queries one at a time. create the temp table, select into it, then drop it as three separate queries. as a security precaution, the sql socket only allows one query in an execution block
It would look something like...