分别获取MySQL列值

时间:2014-03-11 10:13:37

标签: php mysql

我的MySQL表中有一列:

1st row-> 5,3
2nd row-> 4,5,1,3
3rd row-> 1,2

如何单独获取这些值:

1st row-> 5
1st row-> 3
2nd row-> 4
2nd row-> 5
2nd row-> 1
2nd row-> 3

我试过这个$stmt = $conn->prepare("SELECT * FROM users SUBSTRING_INDEX(Following, ',', 1) = Following");但没有工作

1 个答案:

答案 0 :(得分:1)

类似的东西:

$desired_array=array();
$query=mysqli_query($db_conn, "SELECT column_with_commas FROM table_name");
$i=0;
while($array=mysqli_fetch_assoc($query)){
    $row_array=explode(',', $array['column_with_commas']);
    foreach($row_array as $value){
       $desired_array[$i][]=$value;
    }
    $i++;
}

//var_dump($desired_array);

foreach($desired_array as $key => $value){
    for($i=0; $i<count($value); $i++){
         echo 'row '.($key+1).' : '.$value[$i].'<br />';
    }
}

//echo $desired_array[0][0]; //output the first value of row 1
//echo $desired_array[1][0]; //output first value of row 2
//echo $desired_array[0][1]; //out put second value of row 1

输出:

 row 1 : 5
 row 1 : 3
 row 2 : 4
 row 2 : 5
 row 2 : 1
 row 2 : 3
 row 3 : 1
 row 3 : 2