0

我有一个小任务,我有一个 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);
?>
4

5 回答 5

2

您正在基于空间字符进行爆炸,但您的值需要基于,. 所以试试

$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-16T08:56:32.103 回答
0

如果您的类别列包含“22,44,33,55,24,33,22”,那么您的爆炸肯定应该是

$result= explode(",", $value);

例如,在字符串中的逗号上爆炸给你 22, 44, 33 ...

于 2012-10-16T08:53:17.417 回答
0

我认为问题出在$result= explode(" ", $value); 这应该是$result= explode(",", $value);

于 2012-10-16T08:53:37.443 回答
0
$result= explode(",", $value);

由于字符串的格式是用逗号分隔的,所以应该用逗号来展开字符串。

于 2012-10-16T08:54:17.270 回答
0

值是否用逗号分隔?

即使您的问题是数据库设计,explode(',', $value)也应该为您提供 ID。

于 2012-10-16T08:56:09.483 回答