我有一个小任务,其中我有一个mysql表“商店”.It包含一个列“类别”。categories的每个字段都包含不同的值,比如"22,44,33,55,24,33,22“现在从该字段获取每个值,我需要从另一个表中的"parent”列获取该值。(与i链接)我选择了整个字符串,但我想选择每个数字。请帮我弄一下这个。
$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);
?>发布于 2012-10-16 16:56:32
您正在基于空间字符爆炸,但您的价值需要在,的基础上爆炸。所以试一试
$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
}发布于 2012-10-16 16:53:17
如果你的类别列包含"22,44,33,55,24,33,22“,那么你的拆分肯定应该是
$result= explode(",", $value);例如,在字符串中的逗号上爆炸,得到22,44,33 ...
发布于 2012-10-16 16:53:37
我认为问题出在这里,应该是$result= explode(",", $value);
https://stackoverflow.com/questions/12910844
复制相似问题