Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数据库中以逗号分隔的值添加到数组_Php_Arrays - Fatal编程技术网

Php 从数据库中以逗号分隔的值添加到数组

Php 从数据库中以逗号分隔的值添加到数组,php,arrays,Php,Arrays,我的数据库表名为order。它有一行类似于顺序。我像这样存储值1,2,3,4,5。现在我想添加到数组中,并从中输出信息 我试着这么做,但不起作用 这是我的密码: $sql = mysql_query("SELECT * FROM `order` WHERE `id` = ".$_GET['id']." LIMIT 1"); $row = mysql_fetch_assoc($sql); $sql_order = $row['order']; $array = array($sql_order)

我的数据库表名为order。它有一行类似于
顺序
。我像这样存储值
1,2,3,4,5
。现在我想添加到数组中,并从中输出信息

我试着这么做,但不起作用

这是我的密码:

$sql = mysql_query("SELECT * FROM `order` WHERE `id` = ".$_GET['id']." LIMIT 1");
$row = mysql_fetch_assoc($sql);
$sql_order = $row['order'];

$array = array($sql_order);

foreach($array as $x) {
    $sql = mysql_query("SELECT * FROM `product` WHERE `id` = ".$x." LIMIT 1");
    $row = mysql_fetch_assoc($sql);
    $sql_order = $row['order'];

    echo $row['product_name'].'<br />';
}
这个坏了。。我想应该是这样的:
数组([0]=>1[1]=>2[2]=>3[3]=>4[4]=>5)
最快的方法 您需要使用
explode()
函数。下面是一个例子:

<?php
    $array = array('0' =>'1,2,3,4,5');
    $array = explode(',', $array[0]);
    var_dump($array);
?>
您可以根据需要更改列和表的名称

  • 相应地移动旧数据
  • 使用查询
    从order中选择order\u suborders.order\u id,order\u suborders,其中order.id=“.$\u GET['id']”和order.id=order\u suborders.parent\u order

您可以使用explod与“,”进行拆分

$sql=mysql\u query(“从'order'中选择*,其中'id`=”$\u GET['id'].”LIMIT 1”);
$row=mysql\u fetch\u assoc($sql);
$sql_order=$row['order'];
//$array=array($sql\u顺序);
$array=explod(“,”,$sql\u顺序);
foreach($x数组){
$sql=mysql\u查询(“从`product`中选择*其中`id`=”$x.“限制1”);
$row=mysql\u fetch\u assoc($sql);
$sql_order=$row['order'];
echo$row['product_name'].
; }
规范化您的数据库,那么您也不会遇到这样的问题@Rizier123我想输出这样的$array
array([0]=>1[1]=>2[2]=>3[3]=>4[4]=>5)
帮我做这个plzz…如果你规范化你的数据库,你甚至不会有这个问题!因此,规范化数据库,不要在一个字段中存储多个数据!我认为这确实是误导,因为OP最大的问题是,他的数据库没有规范化。这样他就不会从根本上解决问题,如果他不把它正常化,他会遇到越来越严重的问题。是的,我知道他没有把它正常化,这一点也补充道。但这个解决方案正是他想要的(虽然不推荐)。可能是因为他不知道规范化的DB是什么样子,这就是OP要求这个的原因。嗯,也许,好吧,等等,也添加规范化方法。虽然我们不能假设他需要帮助使他的表正常化,但这可能对他有用。:)如果OP的数据库正常化,他甚至不会有这个问题。这并不能解决OP的真正问题。是的,我完全同意你的说法。他需要使他的数据库正常化。这是我的错。
<?php
    $array = array('0' =>'1,2,3,4,5');
    $array = explode(',', $array[0]);
    var_dump($array);
?>
$sql = mysql_query("SELECT * FROM `order` WHERE `id` = ".$_GET['id']." LIMIT 1");
$row = mysql_fetch_assoc($sql);
$sql_order = $row['order'];

$array = array($sql_order);
$array = explode(',', $array[0]);

foreach($array as $x) {
    $sql = mysql_query("SELECT * FROM `product` WHERE `id` = ".$x." LIMIT 1");
    $row = mysql_fetch_assoc($sql);
    $sql_order = $row['order'];

    echo $row['product_name'].'<br />';
}
|   COLUMN   |    TYPE     | 
|:-----------|------------:|
|parent_order|      int    |     
| order_id   |      int    |
  $sql = mysql_query("SELECT * FROM `order` WHERE `id` = ".$_GET['id']." LIMIT 1");
    $row = mysql_fetch_assoc($sql);
    $sql_order = $row['order'];

    //$array = array($sql_order);
    $array=explod(",",$sql_order);

    foreach($array as $x) {
        $sql = mysql_query("SELECT * FROM `product` WHERE `id` = ".$x." LIMIT 1");
        $row = mysql_fetch_assoc($sql);
        $sql_order = $row['order'];

        echo $row['product_name'].'<br />';
    }