SQL查询在where子句上无法正常工作

时间:2015-02-03 09:16:21

标签: php mysql sql

更新:好的,所以{I $id因某种原因是一个字符串,而我正期待一个int。我已将代码更改为:

$id = (int) $id;
      $query = "SELECT funding_opportunities.id
FROM funding_opportunities
  JOIN funders ON funding_opportunities.funder = funders.id
WHERE funders.id='$id'";

var_dump($id)返回int(1)并回显返回的查询

SELECT funding_opportunities.id FROM funding_opportunities JOIN funders ON funding_opportunities.funder = funders.id WHERE funders.id='1'

用于查询

$id = (int) $id;
      $query = "SELECT funding_opportunities.id
FROM funding_opportunities
  JOIN funders ON funding_opportunities.funder = funders.id
WHERE funders.id='$id'";
        var_dump($id);
echo $query;

但我仍然得到同样的错误

我正在尝试从我的PHP文件中执行以下SQL查询:

"SELECT funding_opportunities.id 
FROM funding_opportunities 
  JOIN funders ON funding_opportunities.funder = funders.id 
WHERE funders.id=".$id

返回以下错误:

  

错误号码:1064 您的SQL语法出错;检查   手册,对应右边的MySQL服务器版本   在 1

行<'strong> 1 '附近使用的语法

我做了以下事情:

$id替换为1整数,以检查我的变量中是否存在不可见的错误。

使用了几个在线SQL语法检查工具,没有检测到任何内容。

从PhpMyAdmin重建查询,正确检索值。

有人能发现错误吗?

7 个答案:

答案 0 :(得分:0)

如果ID为int -

,请尝试此操作
WHERE funders.id='$id'"

如果ID是字符 -

WHERE funders.id=\"".$id."\""

答案 1 :(得分:0)

根据ID的组成,您可能需要单引号来验证字符串值,即:

"SELECT funding_opportunities.id 
FROM funding_opportunities 
JOIN funders 
ON funding_opportunities.funder = funders.id
WHERE funders.id= '".$id."'"

答案 2 :(得分:0)

使用此查询。您没有加入$ id来查询部分

"SELECT funding_opportunities.id FROM funding_opportunities JOIN funders ON funding_opportunities.funder = funders.id WHERE funders.id='".$id."'";

答案 3 :(得分:0)

你应该使用像这样的查询

"SELECT funding_opportunities.id 
FROM funding_opportunities 
JOIN funders ON funding_opportunities.funder = funders.id 
WHERE funders.id='".$id."' "

答案 4 :(得分:0)

尝试以下sql

"SELECT funding_opportunities.id 
FROM   funding_opportunities 
JOIN   funders 
ON     funding_opportunities.funder = funders.id 
WHERE  funders.id='".$id."'";

答案 5 :(得分:0)

试试这个

"SELECT funding_opportunities.id 
FROM funding_opportunities 
  INNER JOIN funders ON funding_opportunities.funder = funders.id 
WHERE funders.id=".$id

您忘了指定要使用的JOIN。

答案 6 :(得分:0)

如果变量是整数

,则无需连接
$sql = "SELECT funding_opportunities.id 
        FROM funding_opportunities 
        JOIN funders ON funding_opportunities.funder = funders.id 
        WHERE funders.id=$id";