2010-07-28 6 views
22

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; 

risposta

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

output come questo:

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

su voto come più succinto grazie @will! – KryptoniteDove

+3

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; } ' –

13

Come accennato, json_encode vi aiuterà. Il modo più semplice è quello di recuperare i risultati come già fatto e creare un array che può essere passato a json_encode.

Esempio:

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

tuo $json sarà una serie regolare con ogni elemento in un suo indice.

Ci dovrebbe essere molto poco cambiato nel codice precedente, alternativamente, è possibile restituire sia XML che JSON poiché la maggior parte del codice è la stessa.

+1

fetch assoc per una maggiore piacere. – Lodewijk

14

ecco come ho fatto il mio feed 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

Se avete mysqlnd estensione installata + abilitato in PHP, è possibile utilizzare:

$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() ha bisogno del driver mysqlnd essere installato prima di poterlo utilizzare.

2

sono riuscito a eseguire questo codice:

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

Questa risposta non sembra aggiungere nulla di nuovo alle altre risposte. Tutte le altre risposte sembrano utilizzare la funzione json_encode –