开发者

PHP nesting quotes

开发者 https://www.devze.com 2023-01-27 16:59 出处:网络
The following fails: $result = mysql_query(\"SELECT * FROM Tasks WHERE UserID = \'$_SESSION[\'userID\']\'\");

The following fails:

$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '$_SESSION['userID']'");

I tried the following:

$us开发者_开发知识库erID = $_SESSION['userID'];
$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '$userID'");

and it works. Is there a way to do this without making a separate variable?

Thanks!


Or like this:

$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '{$_SESSION['userID']}'");


$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '".$_SESSION['userID']."'");

or

$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '{$_SESSION['userID']}'");

worth noting it would recommend the first one because it gets easier to read/find when you use a php editor, which in return makes it easier to debugg


Your first chokes the query, because you're actually commanding WHERE userID is '$_SESSION['. Not to mentions that rest which is userID']}' will be interpreted as a syntax error by MySQL.


Yes, like this

$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '$_SESSION[userID]'");
0

精彩评论

暂无评论...
验证码 换一张
取 消