我的 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");
但不起作用
最佳答案
类似的东西:
$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
关于php - 单独获取MySQL列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22322600/