Ho una query mysqli che ho bisogno di formattare come json per un'applicazione mobile.convertire mysqli risultato in json
Sono riuscito a produrre un documento xml per i risultati della query, tuttavia sto cercando qualcosa di più leggero. (vedi sotto per il mio attuale codice xml)
Qualsiasi aiuto o informazione ha apprezzato molto la gente!
$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;
su voto come più succinto grazie @will! – KryptoniteDove
Da utilizzare per PHP 7.1.x per recuperare l'intera riga come oggetto se è necessario recuperare più di una colonna. 'while ($ row = $ res-> fetch_object()) { $ myArray [] = $ row; } ' –