[英]How come my query works in phpmyadmin but not in my php?
I have a query I'm trying to run in my php code and it is only returning one result, but if i run the same query in phpmyadmin it works. 我有一个查询,我试图在我的php代码中运行,并且它只返回一个结果,但是如果我在phpmyadmin中运行相同的查询,它就可以工作。 Can anyone tell me where I'm going wrong? 谁能告诉我我要去哪里错了?
<?php
$sql = "SELECT * FROM `product_packs` WHERE `name` IN('" . implode("', '", $_SESSION['cart_items']) . "')";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<div class='col-xs-6 col-sm-4 col-md-2 col-lg-2'>
<div class='products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='remove_from_cart.php?name=" . $row['name'] . "&price=" . $row['price'] . "'>
Remove From Cart
</a>
</p>
</div>
</div>
</div>";
}
?>
I have printed the query to make sure the result of the implode is correct and it seems to be as i can run the result in phpmyadmin and it works fine. 我已经打印了查询,以确保爆破的结果是正确的,并且似乎可以在phpmyadmin中运行结果,并且工作正常。
Any help would be appreciated. 任何帮助,将不胜感激。
Try the following (NOT tested as far as I have not sufficent data provided form you): 尝试以下方法(就我尚未提供足够的数据而言,尚未测试):
UPDATED: 更新:
<?php
$sql = "SELECT * FROM `product_packs` WHERE `name` IN('" . implode("', '", $_SESSION['cart_items']) . "')";
// dump the query send to the database
$var_dump($sql);
$result = $conn->query($sql);
if ($result->num_rows > 0){
$resultset_count = $result->num_rows;
// dump the number of resultsets in the query
var_dump($resultset_count);
while($row = $result->fetch_assoc()){
echo "<div class='col-xs-6 col-sm-4 col-md-2 col-lg-2'>
<div class='products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='remove_from_cart.php?name=" . $row['name'] . "&price=" . $row['price'] . "'>Remove From Cart </a>
</p>
</div>
</div>
</div>
</div>";
}
?>
Why don't you use FIND_IN_SET
? 您为什么不使用FIND_IN_SET
? It will work for you, I don't know your database structure but I have created an example query that could be of help for you 它将为您工作,我不知道您的数据库结构,但是我创建了一个示例查询,可能会对您有所帮助
why don't use FIND_IN_SET? 为什么不使用FIND_IN_SET? it will work for you i don't know your database structure still i have created query for you it might help you 它会为您工作我不知道您的数据库结构还是为您创建了查询,它可能会为您提供帮助
SELECT * FROM product_packs
WHERE (
FIND_IN_SET(cart_items, (SELECT cart_items TABLENAME
WHERE cart_items = '$_SESSION['cart_items']')
)
)
ORDER BY `product_id` ASC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.