【问题标题】:How can i read this column from my database [duplicate]我如何从我的数据库中读取此列 [重复]
【发布时间】:2019-07-01 07:32:08
【问题描述】:

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

列名为Actual,表格如下所示

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

感谢任何帮助

我的尝试

测试1

  $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() on 数组

【问题讨论】:

  • 那么,print_r 的结果是什么?
  • 用 print_r 结果编辑我的问题
  • 使用$db-&gt;query("SELECT Actual FROM csvhoejde1");而不是$db-&gt;prepare()$db-&gt;execute(),因为你不需要绑定任何值
  • 如果只是你得到两组值 - 试试fetchAll(PDO::FETCH_COLUMN, 0)
  • 谢谢!成功了!

标签: php pdo


【解决方案1】:
$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;

【讨论】:

  • 我不能真正使用它,因为它使用了 mysqli_fetch_assoc。不适用于我的代码。但我相信它适用于那些有能力的人。
猜你喜欢
  • 2012-02-23
  • 2012-01-09
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 2021-08-19
  • 2019-07-03
  • 1970-01-01
  • 2016-06-16
相关资源
最近更新 更多