[英]PHP / SQL Query and PHP Variables
I have this query below: 我在下面有这个查询:
$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id='someid'
ORDER BY id ASC
LIMIT $display_num",$myconn);
see this line: AND id='someid' <-- someid ... 看到这一行: AND id ='someid'<-someid ...
OK, the query above returns 2 results as expected... OK,上面的查询按预期返回2个结果...
Now for the problem.... 现在解决问题。
-- I have a variable myVar and it's content is " someid " (without quotes)...same as the string ' someid ' -我有一个变量myVar,其内容为“ someid ”(不带引号)...与字符串“ someid ”相同
When I do this: 当我这样做时:
$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id=myVar
ORDER BY id ASC
LIMIT $display_num",$myconn);
See: myVar <-- this variable contans .. someid 请参阅:myVar <-此变量包含.. someid
The second query returns no results. 第二个查询不返回任何结果。
Update: When using ... AND id='$myVar'
it sees $myVar
as empty for some reason. 更新:使用
... AND id='$myVar'
,出于某些原因,它会将$myVar
视为空。
Put a $ in front of myVar: 在myVar前面放一个$:
$msgg = mysql_query(
"SELECT *
FROM mytable
WHERE time > $time
AND id = '$myVar'
ORDER BY id ASC
LIMIT $display_num", $myconn
);
You forgot the dollar sign and the single quotations: 您忘记了美元符号和单引号:
AND id='$myVar'
Additionally, you may want to consider using heredoc : 此外,您可能要考虑使用heredoc :
$query = <<<MYSQL
SELECT *
FROM mytable
WHERE time>$time
AND id='$myVar'
ORDER BY id ASC
LIMIT $display_num
MYSQL;
$msgg = mysql_query($query, $myconn);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.