【问题标题】:Pulling data from MySQL into json array将数据从 MySQL 拉入 json 数组
【发布时间】:2012-04-28 09:42:28
【问题描述】:

我正在尝试使用 php 中的 json 从我的数据库中提取数据。我有一些元素需要具体说明,然后才能将它们发布到页面上。

我想从 mysql 中“获取”数据并将其返回到 json_encode。如何使用 SELECT 方法执行此操作。有些人使用了 PDO 方法,有些人使用了 mysql_assoc,这让我很困惑。

例如,

我有以下行:'id'、'title'、'start'、'backgroundColor'...等。以及所有这些的默认值。 ($array[] = "someValue = default")

我希望它像这样导出:

array(
      'id' => 1,
      'title' => "someTitle",
      'start' => "2012-04-16",
      'backgroundColor' => "blue",
      'someValue' = > "default",
      ...
      ),   ....
 ));

如果有人能用最好的细节帮助我,我会很棒!

【问题讨论】:

    标签: php mysql json


    【解决方案1】:

    如果您想使用 PDO 执行此操作,那么这里有一个示例:

    <?php 
    $dbh = new PDO("mysql:host=localhost;dbname=DBNAME", $username, $password);
    
    $sql = "SELECT `id`, `title`, `time`, `start`, `backgroundColor` 
            FROM my_table";
    
    $result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    //To output as-is json data result
    //header('Content-type: application/json');
    //echo json_encode($result);
    
    //Or if you need to edit/manipulate the result before output
    $return = [];
    foreach ($result as $row) {
        $return[] = [ 
            'id' => $row['id'],
            'title' => $row['title'],
            'start' => $row['start'].' '.$row['time'],
            'backgroundColor' => $row['backgroundColor']
        ];
    }
    $dbh = null;
    
    header('Content-type: application/json');
    echo json_encode($return);
    ?>
    

    【讨论】:

    • 为什么要指定header('Content-type: application/json');
    • 如果您要使用 jQuery ajax() 或类似的方式访问输出,您需要指定标头内容类型以匹配 dataType.api.jquery.com/jQuery.ajax(DataType 部分)也是设置内容类型的好习惯除了text/html
    • 好的。这种方法可以轻松获取所有数据。如果我想用空格将两个数据添加到一个数据中怎么办? "数据" => $row["start"]." ".$row["time"],
    • 我已经更新了我的答案。如果您想在将值分配给数组之前对数据进行操作,那么您需要循环它,有足够的示例可以让您了解现在要做什么。
    • 这实际上使您像这样写出来更容易使用。我需要做的就是为我的密码和用户名设置变量并添加剩余的数组获取。但是,如果我想留下空白的项目,而不是空的。我可以指定,“FROM my_table where length(column) > 0”或者我该怎么说?
    【解决方案2】:

    你可以从mysql中获取结果,然后将其格式化为json

        $array = array();
        while($row = mysqli_fetch_array($result))
        {
            array_push($array,$row);
        }
        $json_array = json_encode($array);
    

    【讨论】:

    • 这有什么用? $row 将仅包含被感染的最后一行。之后也不能将多个独立编码的字符串连接成一个 json 字符串。
    【解决方案3】:

    请检查SELECT方法here

    一般来说是这样的

    $data = array(); // result variable
    
    $i=0
    
    $query = "SELECT id,title,start,backgroundColor FROM my_table"; // query with SELECT
    
    $result = mysql_query($query);
    
    
    while($row = mysql_fetch_assoc($result)){ // iterate over results
          $data['item'][$i]['id'] = $row['id']; // rest similarly 
          ...
          ...
    
          $i++; 
    }
    
    header('Content-type: application/json'); // display result JSON format
    echo json_encode(array(
         'success' => true,
         'data' => $data // this is your data variable
    ));
    

    【讨论】:

    • 您将所有行提取到一个 SINGLE 变量中,只留下最后提取的行在 json 字符串中发送出去。
    【解决方案4】:

    您不会“获取到 json 数组”。

    您将数据库结果提取到 PHP 数组中,然后在提取完成后将该 php 数组转换为 json 字符串。

    例如

    $data = array();
    while ($row = mysql_fetch_assoc($results)) {
       $data[] = $row;
    }
    echo json_encode($data);
    

    【讨论】:

    • 如果你想使用 PDO,你可以跳过 while:$data = $queryHandle->fetchAll();
    • 所以 $data[] 类似于 ` $d​​ata['id'] = $row['id'];` ?
    • 没有。 $data 将是一个数组数组,每个子数组都是一次 fetch 操作的结果。
    • @Marc B 所以这将是开始,因此之后,你会像我说的那样指定每个数组吗?
    • 获取第 8 行的 id 字段应该是 data[7]['id']
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2017-06-04
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多