将mysqli结果转换为json

时间:2010-07-28 10:16:52

标签: php json mysqli

我有一个mysqli查询,我需要格式化为移动应用程序的json。

我已经设法为查询结果生成一个xml文档,但是我正在寻找更轻量级的东西。 (请参阅下面的我当前的xml代码)

任何帮助或信息都非常感谢人们!

$mysql = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME) or die('There was a problem connecting to the database');

$stmt = $mysql->prepare('SELECT DISTINCT title FROM sections ORDER BY title ASC');
$stmt->execute();
$stmt->bind_result($title);

// create xml format
$doc = new DomDocument('1.0');

// create root node
$root = $doc->createElement('xml');
$root = $doc->appendChild($root);

// add node for each row
while($row = $stmt->fetch()) : 

     $occ = $doc->createElement('data');  
     $occ = $root->appendChild($occ);  

     $child = $doc->createElement('section');  
     $child = $occ->appendChild($child);  
     $value = $doc->createTextNode($title);  
     $value = $child->appendChild($value);  

     endwhile;

$xml_string = $doc->saveXML();  

header('Content-Type: application/xml; charset=ISO-8859-1');

// output xml jQuery ready

echo $xml_string;

6 个答案:

答案 0 :(得分:64)

$mysqli = new mysqli('localhost','user','password','myDatabaseName');
$myArray = array();
if ($result = $mysqli->query("SELECT * FROM phase1")) {

    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $myArray[] = $row;
    }
    echo json_encode($myArray);
}

$result->close();
$mysqli->close();
  1. $row = $result->fetch_array(MYSQLI_ASSOC)
  2. $myArray[] = $row
  3. 输出如下:

    [
        {"id":"31","name":"pruduct_name1","price":"98"},
        {"id":"30","name":"pruduct_name2","price":"23"}
    ]
    

    如果您想要其他款式,可以试试这个:

    1. $row = $result->fetch_row()
    2. $myArray[] = $row
    3. 输出将是这样的:

      [
          ["31","pruduct_name1","98"],
          ["30","pruduct_name2","23"]
      ]
      

答案 1 :(得分:16)

这是我如何制作我的json feed:

    $mysqli = new mysqli('localhost','user','password','myDatabaseName');
    $myArray = array();
    if ($result = $mysqli->query("SELECT * FROM phase1")) {
        $tempArray = array();
        while($row = $result->fetch_object()) {
                $tempArray = $row;
                array_push($myArray, $tempArray);
            }
        echo json_encode($myArray);
    }

    $result->close();
    $mysqli->close();

答案 2 :(得分:13)

如上所述,json_encode会对您有所帮助。最简单的方法是获取您已经完成的结果并构建一个可以传递给json_encode的数组。

示例:

$json = array();
while($row = $stmt->fetch()){
  $json[]['foo'] = "your content  here";
  $json[]['bar'] = "more database results";
}
echo json_encode($json);

您的$json将是一个常规数组,其中每个元素都有自己的索引。

上面的代码应该没有什么变化,因此,你可以返回XML和JSON,因为大多数代码是相同的。

答案 3 :(得分:3)

我设法运行此代码:

<?php
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
return json_encode($emparray);
?>

答案 4 :(得分:0)

如果您在php中安装了mysqlnd扩展程序+,则可以使用:

$mysqli = new mysqli('localhost','user','password','myDatabaseName');

$result = $mysqli->query("SELECT * FROM phase1");

//Copy result into a associative array
$resultArray = $result->fetch_all(MYSQLI_ASSOC);

echo json_encode($resultArray);
  

mysqli :: fetch_all()需要先安装mysqlnd驱动程序   可以使用它。

答案 5 :(得分:0)

关于JSON有一件重要的事情-数据必须必须采用UTF-8编码。因此,必须为数据库连接设置正确的编码。

其余的与其他任何数据库操作一样愚蠢

$mysql = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_NAME);
$mysql->set_charset('utf8mb4');

$sql = 'SELECT DISTINCT title FROM sections ORDER BY title ASC';
$data = $mysql->query(sql)->fetch_all(MYSQLI_ASSOC);
echo json_encode($data);