php - 单独获取MySQL列值

标签 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"); 但不起作用

最佳答案

类似的东西:

$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/

相关文章:

php邮件重复

javascript - 输入值/仅启用最后 10 个字符

php - 按 ID 搜索,但在下拉菜单中显示行信息

php - 在 MYSQL 中使用 like 语句而不考虑顺序

php - 处理 php 中的特定异常

php - PDO 结果到数组

php - 设置默认 View 值标志时 Magento 的行为很奇怪

mysql - 在 MySQL 中存储用户偏好的最佳方式 - BINARY 或 INT

mysql - 如何在Linux中输入sql命令时出现提示?

mysql - 存储琐事替代品的最佳方式?