doumeba0486 2015-08-30 21:30
浏览 30
已采纳

用于编码JSON中的DB数据的PHP代码不起作用

I'm currently stuck with some PHP code. I want to access a table in my database and retrieve the data in a JSON format. Therefore, I tried the following code :

<?php

$con = mysqli_connect("......","username","pwd","DBName");

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "SELECT * FROM users";

if ($result = mysql_query($con, $sql))
{

    $resultArray = array();
    $tempArray = array();    

    while($row = $result->fetch_object())
    {
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    echo json_encode($resultArray);
}

mysqli_close($con);
?>

However, it's getting me an empty page. It worked once but only with a special number of row in the table, so not very efficient as you might guess.

Does anybody have an idea why i'm getting those weird results?

EDIT 1 :

I Just tried to add this to my code :

echo json_encode($resultArray);
echo json_last_error();

And it's returning me 5. It seems to be an error from the data encoding in my table. Therefore I added that code :

$tempArray = array_map('utf8_encode', $row)
array_push($resultArray, $tempArray);

And I got the following output : [null,null,null]0 (The zero comes from the echo json_last_error();)

So here I am, can anybody help me with this ?

  • 写回答

4条回答 默认 最新

  • doushu7588 2015-09-03 14:03
    关注

    I finally found a solution ! That was indeed an encoding problem, the json_encode() function accepts only strings encoded in utf8. I changed the interclassement of my table to utf8_general_ci and I modified my code as follows :

    <?php
    //Create Database connection
    
    $db = mysql_connect(".....","username","pwd");
    
    if (!$db) {
    
        die('Could not connect to db: ' . mysql_error());
    
    }
    
    
    //Select the Database
    
    mysql_select_db("DBName",$db);
    
    
    
    //Replace * in the query with the column names.
    
    $result = mysql_query("SELECT * FROM users", $db); 
    
    
    //Create an array
    
    $json_response = array();
    
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    
        $row_array['id'] = $row['id'];
        $row_array['name'] = utf8_encode($row['name']);
        $row_array['lastName'] = utf8_encode($row['lastName']);
    
        //push the values in the array
    
        array_push($json_response,$row_array);
    
    }
    
    echo json_encode($json_response);
    //Close the database connection
    
    fclose($db);
    
    ?>
    

    And I got the expected output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算