我如何从数据库中读取此列

时间:2019-02-07 08:21:42

标签: php pdo

我的数据库中有一列遇到问题。

该列名为Actual,该表如下所示:

enter image description here

我需要像这样阅读。 [129.74,130.74,129.50]

感谢您的帮助

我尝试过的事情

Test1     

  $sth = $db->prepare("SELECT Actual FROM csvhoejde1");
  $sth->execute();

  /* Fetch all of the remaining rows in the result set */
  $result = $sth->fetchAll();
  echo'<pre>';
  print_r($result);
  echo'</pre>';
?>

print_r结果:

Array
(
[0] => Array
    (
        [Actual] => 129.74
        [0] => 129.74
    )

[1] => Array
    (
        [Actual] => 130.74
        [0] => 130.74
    )

测试2

  <?php
$sql = "SELECT `Actual` FROM `csvhoejde1`";
$test = $db->query($sql)->fetchAll(PDO::FETCH_COLUMN);

echo'<pre>';
print_r($test);
echo'</pre>';
?>

结果:

  

致命错误:未捕获错误:调用成员函数fetchAll()   数组

1 个答案:

答案 0 :(得分:0)

$upit = "SELECT Actual FROM csvhoejde1";

$conn = new mysqli('localhost','user','pass','db');

$result = $conn->query($upit);

$array = [];

while ($r = mysqli_fetch_assoc($result)){
    $array[] = $r;
}

$string = "[";


    foreach ($array as $ar){
        $string .= $ar['Actual'].",";
    }


    $string = rtrim($string,',')."]";

    echo $string;