Php循环遍历数组以从数据库中进行选择

时间:2014-01-29 13:21:45

标签: php mysql arrays foreach

我有一个数字列表,我将其分解为一个数组。然后我想遍历数组并使用数组中的值从数据库中调用信息。我尝试使用mysqli_data_seek重置结果,但第二个循环抛出错误。

<?php 
$relatedArt = explode(",",$art["art_related"]); 

foreach($relatedArt as $value) { 

$relatedQ = "SELECT art_id,art_name,art_thumb FROM zoo_art WHERE art_id = ".$value." "; 

$relatedR = mysqli_query($connection, $relatedQ); 

confirm_query($relatedR, "problem"); 

mysqli_data_seek($relatedR, 0);                             
} 
?>

1 个答案:

答案 0 :(得分:1)

你应该做这样的事情而不是foreach循环。

$relatedArt = explode($art["art_related"]);
$values = implode(",",$relatedArt); 
$relatedQ = "SELECT art_id,art_name,art_thumb FROM zoo_art WHERE art_id IN(".$values.") ";

IN Operator用于以任何给定的顺序查询数据库。