2010-07-28 97 views
22

我有一個mysqli查詢,我需要格式化爲json的移動應用程序。將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; 

回答

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

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

$result->close(); 
$mysqli->close(); 
  1. $row = $result->fetch_array(MYSQL_ASSOC)
  2. $myArray[] = $row

輸出是這樣的:

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

投票作爲最簡潔的感謝@will! – KryptoniteDove 2015-03-15 21:34:33

+3

如果您必須檢索多個列,請將此用於PHP 7.1.x,以將整行作爲對象檢索。 'while($ row = $ res-> fetch_object()){$ myArray [] = $ row; }' – 2017-10-10 01:25:16

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,因爲大部分代碼都是相同的。

+1

取增加樂趣副教授。 – Lodewijk 2014-08-12 00:33:06

14

這裏就是我做了我的JSON提要:

$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(); 
0

如果您mysqlnd擴展安裝+ php中啓用後,您可以使用:

$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驅動程序安裝之前,你 可以使用它。

2

我設法運行此代碼:

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

這個答案似乎沒有增加任何新的東西給其他答案。所有其他的答案似乎是使用json_encode函數 – 2017-07-01 23:56:02