我只是一个初学者,我正在做一个项目(购物车)。用户可以将产品添加到购物车以及 session 中产品商店的 ID。当我使用这些 ID 从数据库中回显 PRICE 时,它不起作用。我正在使用 PHP 和 MYSQL。这是我的代码
if(count($_SESSION['cart_items'])>0){
// getting the product ids
$nos = "";
foreach($_SESSION['cart_items'] as $no=>$value){
$nos = $nos . $no . ",";
}
// removing the last comma
$nos = rtrim($nos, ',');
//echo $nos; (will display like this INT VALUES 1,2,3,4)
$nos=mysql_real_escape_string($nos);
$site4->DBlogin();
$qry = "SELECT * FROM vendorproducts WHERE product_no IN('.implode(',',$nos).')";
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
echo $row['price'];
}
最佳答案
PHP 不可递归嵌入:
$qry = "SELECT * FROM vendorproducts WHERE product_no IN('.implode(',',$nos).')";
^---start of string end of string ---^
由于您已经在字符串中,因此 .implode(...)
只是纯文本,不是可执行代码。
这意味着您的查询是非法/无效的 SQL,如果您进行了基本/最小错误检查,就会被告知:
$result = mysql_query($qry) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^
关于php - 使用一组值(从 session 数组存储的值)的 MYSQL 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569756/