这个问题很简单,但我不确定涉及的术语,可以自己查找答案。我拥有的是一个包含我所有产品信息的 MYSQL 数据库。我想让图像出现在具有特定属性的产品的产品页面上。我创建了这个 SQL 查询,它输出我希望图像显示的产品的 Product_ids 列表。但是,我不知道如何在页面本身中使用这组结果。
我目前有:
$cupwinnerids = mysql_query("SELECT product_id FROM `jos_vm_product_type_1` WHERE jos_vm_product_type_1.Cup_Winner ='Cup Winners';");
while($row = mysql_fetch_array($cupwinnerids))
{
echo $row['product_id'] . ",";
}
这会输出正确的 id,它们之间有一个逗号。我想做的就是用 $listofids = (...) 之类的东西包装整个内容,然后我可以在产品页面 PHP 文件中使用:如果 $product_id 在 $listofids 中,那么...如果不是那么.. .我只是在理解如何使用此选择的 id 时遇到问题。如果我尝试直接输出列表,我只会得到“数组”。任何帮助将不胜感激。
最佳答案
问题可能是您尝试使用 echo 显示数组,而您应该使用 print_r。
在你的情况下,你可以这样做:
$listofids = array();
$cupwinnerids = mysql_query("SELECT product_id FROM `jos_vm_product_type_1` WHERE jos_vm_product_type_1.Cup_Winner ='Cup Winners';");
while($row = mysql_fetch_array($cupwinnerids))
{
array_push($listofids, $row['product_id']);
}
print_r($listofids);
正如 Ryan 所评论的,如果你想管理数组的值,你应该使用 foreach 循环,迭代数组,就像这样
foreach ($listofids as $id) {
echo $id . ", ";
}
// EDIT: you could also use echo implode($listofids,", ") which will do basically the same
如果您想要比较 $product_id 是否在 $listofids us in_array 中,则如果您要查找的值在数组中,则返回 true,如果不在数组中,则返回 false:
if (in_array($product_id, $listofids)) {
echo "Product ID is in the List of Product ID's";
}
else {
echo "Product ID isn't in the List of Product ID's";
}
关于php - 在PHP中使用一组Mysql结果作为比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9650603/