我有一個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;
投票作爲最簡潔的感謝@will! – KryptoniteDove 2015-03-15 21:34:33
如果您必須檢索多個列,請將此用於PHP 7.1.x,以將整行作爲對象檢索。 'while($ row = $ res-> fetch_object()){$ myArray [] = $ row; }' – 2017-10-10 01:25:16