简体   繁体   English

查询在SQL中有效但在PHP中无效

[英]Query that works in SQL but not in PHP

I am having trouble with an SQL query that I have inserted into a piece of PHP code to retrieve some data. 我遇到了一个SQL查询问题,我已经插入到一段PHP代码中来检索一些数据。 The query itself works perfectly within SQL, but when I use it within my PHP script it says "Error in Query" then recites the entire SQL statement. 查询本身在SQL中完美运行,但是当我在我的PHP脚本中使用它时,它会显示“查询错误”,然后背诵整个SQL语句。 If I copy and paste the SQL statement from the error message directly into MySQL it runs with no errors. 如果我将错误消息中的SQL语句直接复制并粘贴到MySQL中,则运行时没有错误。

From my research I believe I am missing an apostrophe somewhere, so PHP may be confusing the clauses, but I am not experienced enough to know where to insert them. 根据我的研究,我相信我错过了某个地方的撇号,因此PHP可能会混淆条款,但我没有足够的经验知道在哪里插入它们。

The query is using a variable called $userid which is specified earlier in the PHP script. 该查询使用的是一个名为$ userid的变量,该变量在PHP脚本中先前已指定。

$sql= <<<END

SELECT sum(final_price)  
FROM (
    SELECT Table_A.rated_user_id, Table_B.seller, Table_B.final_price
    FROM Table_A
       INNER JOIN Table_B ON Table_A.id=Table_B.id
) AS total_bought
WHERE seller != $userid
AND rated_user_id = $userid

    UNION ALL

SELECT sum(final_price)  
FROM (
    SELECT Table_A.rated_user_id, Table_C.seller, Table_C.final_price
    FROM Table_A
        INNER JOIN Table_C ON Table_A.id=Table_C.id
) AS total_bought
WHERE seller != $userid
AND rated_user_id = $userid

END;

After this section the script then goes on to define the output and echo the necessary pieces as per usual. 在此部分之后,脚本继续定义输出并按照惯例回显必要的部分。 I'm happy with the last part of the code as it works elsewhere, but the problem I am having appears to be within the section above. 我很满意代码的最后一部分,因为它在其他地方工作,但我遇到的问题似乎在上面的部分内。

Can anyone spot the error? 谁能发现错误?

Edited to add the following additional information: 编辑添加以下附加信息:

All of the fields are numerical values, none are text. 所有字段都是数值,没有一个是文本。 I have tried putting '$userid' but this only makes the error display the ' ' around this value within the error results. 我已经尝试过'$ userid',但这只会让错误在错误结果中显示''这个值。 The issue remains the same. 问题依然如故。 Adding parenthasis has also not helped. 添加学生重点也没有帮助。 I had done a bit of trial and erorr before posting my question. 在发布我的问题之前,我做了一些试验和更多的尝试。

If it helps, the last part of the code bieng used is as follows: 如果有帮助,使用的代码的最后部分如下:

$result = mysql_query($sql);
if (!$res) {
  die('Error: ' . mysql_error() . ' in query ' . $sql);
}
$total_bought = 0;
while ($row = mysql_fetch_array($result)) {
  $total_bought += $row[0];
}
$total_bought = number_format($total_bought, 0);
echo '<b>Your purchases:  '  . $total_bought . '</b>';
echo "<b> gold</b>";

You're checking !$res , it should be !$result : 你正在检查!$res ,应该是!$result

$result = mysql_query($sql);
if (!$result) {
  die('Error: ' . mysql_error() . ' in query ' . $sql);
}

I suppose, you're echo() ing the query somewhere and copy-pasting it from the browser. 我想,你在某处echo()查询并从浏览器中复制粘贴它。 Could it be that the $userid contains xml tags? 可能是$userid包含xml标签吗? They wouldn't be displayed in the browser, you would have to view the page source to spot them. 它们不会显示在浏览器中,您必须查看页面源以发现它们。

你应该用引用的$ userid进行测试,并在两个语句周围加括号。

I'm assuming that rated_user_id is a numeric field, but what type is seller? 我假设rated_user_id是一个数字字段,但卖家是什么类型的? If it's a character field, then $userid would have to be quoted as streetpc suggests. 如果它是一个字符字段,则必须引用$ userid作为streetpc建议。

Another thing to check is that you have at least one space after the end of your lines for each line of the query. 要检查的另一件事是,对于查询的每一行,在行结束后至少有一个空格。 That has tripped me up before. 那之前让我绊倒了。 Sometimes when going from your editor/IDE to the database tool those problems are silently taken care of. 有时从编辑器/ IDE到数据库工具时,这些问题都会被默默地处理掉。

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

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