简体   繁体   English

SQL查询返回错误结果

[英]SQL query is returning the wrong results

At the top of my file I have checked what the $clientid is using echo $clientid . 在文件的顶部,我已经使用echo $clientid检查了$clientid在使用什么。 This echoes 731. 这回音731。

Further down the script I'm running this query: 在脚本的更下方,我正在运行此查询:

$active_ts = db_select("SELECT * FROM `timesheets` WHERE clientid='".$clientid."' AND status=\"client\" OR status=\"cand\"");
var_dump($active_ts); 

What I expect to happen is only timesheets with a clientid of 731 to be chosen. 我希望会发生的事情只是选择了具有731客户clientid的时间表。

What the var_dump shows however is ["clientid"]=> string(3) "345" , ["clientid"]=> string(3) "712" , ["candid"]=> string(3) "730" and ["clientid"]=> string(3) "721" 但是var_dump显示的是["clientid"]=> string(3) "345"["clientid"]=> string(3) "712" ,[“ candid”] => string(3)“ 730”和["clientid"]=> string(3) "721"

I don't really know how to debug this further.... 我真的不知道该如何进一步调试...。

This is your query: 这是您的查询:

SELECT *
FROM `timesheets`
WHERE clientid = '".$clientid."' AND status = 'client' OR status = 'cand'

The WHERE clause has no parentheses. WHERE子句没有括号。 It is interpreted as: 它被解释为:

WHERE (clientid = '".$clientid."' AND status = 'client') OR status = 'cand'

This is presumably not what you want. 大概这不是您想要的。 I would suggest you use IN : 我建议您使用IN

WHERE clientid = '".$clientid."' AND status  IN ('client', 'cand')

Because query select user with status = cand . 因为查询选择status = cand用户。

You need to change your query : 您需要更改查询:

Change : 变更:

$active_ts = db_select('
    SELECT * FROM `timesheets` 
    WHERE clientid='.$clientid.' AND (status="client" OR status="cand")
');

I think you don't need ' between clientid because it's int. 我认为您在clientid之间不需要' ,因为它是int。

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

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