【问题标题】:PHP PDO fetch data from ArrayPHP PDO 从数组中获取数据
【发布时间】:2017-05-13 06:32:37
【问题描述】:

这段代码非常适合 Mysqli

$sql = "SELECT * FROM 'mem_tree' ";
$res = mysqli_query($conn, $sql) or die("database error:".mysqli_error($conn));
while($row = mysqli_fetch_assoc($res)){ 
  $data[] = $row;
}
$itemsByReference = array();

但是当我尝试使用 PDO 时它不起作用

$sql = "SELECT * FROM 'mem_tree' ";
$stmt = $dbh->prepare($sql);
$stmt->execute(); 
while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)){ 
   $data[] = $row;
}
$itemsByReference = array();

【问题讨论】:

  • 您看到了什么错误?请粘贴您的错误
  • “它不起作用”不是问题陈述。见:stackoverflow.com/help/mcve
  • $pdo = new PDO(); ?观看THIS ref. Connection management。另外,在页面顶部使用error_reporting(E_ALL); ini_set('display_errors', 1);,以便 PHP 帮助处理错误
  • 我在 js 树示例中使用此代码,在 PDO 代码中 js 树正在加载和加载,而在 mysqli 中它可以完美运行。

标签: php mysqli pdo


【解决方案1】:

在这里我找到了解决方案

  $sql = "SELECT * FROM mem_tree";
  $stmt = $dbh->prepare($sql);
  $stmt->execute();
  $final_arr = array();
  $data = array();
  $final_arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
  $data = $final_arr;
  $itemsByReference = array();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    相关资源
    最近更新 更多