Why does this MySQL query work in phpmyadmin but doesn't return anything in my php script?
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.开发者_StackOverflow社区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
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...
mysql_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;");
$result = mysql_query("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;
");
精彩评论