PHP在使用SESSION时给出变量错误


PHP giving variable error when using SESSION

全部,我正试图编写一个mySQL查询,但PHP给了我一个错误。给我一个错误的行是:

$qry = "Select * from vendor_options where vendor_option_id='$_SESSION[pav_vendor_categories_$i]'";

上面的代码在for循环中,所以$i就是这样填充的。我收到的错误是:

分析错误:语法错误,意外的T_VARIABLE,应为"]">

有什么问题吗?谢谢

$sVendorId = $_SESSION['pav_vendor_categories_' . $i];
$sQuery = "SELECT * FROM vendor_options WHERE vendor_option_id='{$sVendorId}'";

这是您的工作代码
在查询之外生成供应商选项ID——这将使代码更具可读性。

试试这个;

$qry = "Select * from vendor_options where 
          vendor_option_id='{$_SESSION["pav_vendor_categories_{$i}"]}'";

演示:http://codepad.org/0nHsFZ8i

应为:

$qry = "Select * from vendor_options where vendor_option_id='".$_SESSION["pav_vendor_categories_".$i]."'";

但是不要那样做。。。阅读sql注入

希望这对有帮助