为什么这个 MySQL 查询可以在 phpmyadmin 中工作,但在我的 php 脚本中不返回任何内容?

发布于 2024-10-16 02:51:17 字数 1979 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2024-10-23 02:51:17

尝试一次执行一个查询。创建临时表,选择其中,然后将其作为三个单独的查询删除。作为一项安全预防措施,sql 套接字只允许在执行块中执行一个查询,

它看起来像......


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;");

$结果= mysql_query(“选择node.nid,node.title,users.name,node_counter.totalcount,node_comment_statistics.comment_count,node_comment_statistics.last_comment_timestamp 从节点 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 左连接用户在node.uid = users.uid LEFT JOIN 已解决 ON node.nid=solved.nid WHERE term_node.tid=522 ANDsolved.nid 为 NULL; ”);

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; ");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文