我有一个小任务,我有一个mysql表“商店”。它包含一个列“类别”。每个类别的字段都包含不同的值,如“22,44,33,55,24,33,22” 现在从该字段中获取每个值,我需要从另一个表中的“父”列中获取值。 (与ids链接)我选择整个字符串,但我想选择每个数字。请帮帮我。
$db_selected = mysql_select_db("",$con);
$sql = "SELECT categories from shops";
$array = mysql_query($sql,$con);
while($row=mysql_fetch_array($array)){
foreach($row as $value){
$result= explode(" ", $value);
foreach($result as $newvalue){
$query="SELECT parent FROM categories where categories.id=$newvalue<br/>";
echo $query;
}
}
}
mysql_close($con);
?>
答案 0 :(得分:1)
您正在根据空格字符进行爆炸,但您的价值需要在
,
的基础上展开。所以试试
$result= explode(",", $value);
foreach($result as $newvalue){
$query="SELECT parent FROM categories where categories.id='$newvalue'";
// ^ Quotes the Value
// Remove the <br />
echo $query."<br />"; //Instead add it here and avoid the bug if you decide the run the query
// This example is showing mysql_* library but it is deprecated
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$parent = $row['parent']; //Now you can something like this
}
答案 1 :(得分:0)
如果您的类别列包含“22,44,33,55,24,33,22”,那么您的爆炸肯定应该是
$result= explode(",", $value);
例如,在字符串中的逗号上爆炸,为你提供22,44,33 ......
答案 2 :(得分:0)
我认为问题出在$result= explode(" ", $value);
它应该是$result= explode(",", $value);
答案 3 :(得分:0)
$result= explode(",", $value);
由于字符串的格式用逗号分隔,因此应该用逗号分隔字符串。
答案 4 :(得分:0)
值是否以逗号分隔?
即使您的问题是数据库设计,explode(',', $value)
也应该为您提供ID。