We shouldn't see any use of 0 or pdo functions.Explicitly calling 1 before outputting your data payload is considered to be best practice by some devs. This is usually not a requirement, but clarifies the format of the payload to whatever might be receiving it.Assuming this is the only data being printed, it is safe to print the json string using 2 which will terminate the execution of the script as well. This, again, is not essential because 3 will work just as well, but some devs consider it a good practice to explicitly terminate the script.MySQLi single-row result set from query result set object:
MySQLi multi-row result set from query result set object:
MySQLi single-row result set from prepared statement:
MySQLi multi-row result set from prepared statement:
PDO single-row result set from query result set object:
PDO multi-row result set from query result set object:
PDO single-row result set from prepared statement:
PDO multi-row result set from prepared statement:
Obey these rules to prevent the possibility of generating invalid json.:
If you need to iterate your result set data to run php functions or provide functionality that your database language doesn't offer, then you can immediately iterate the result set object with 6 and access values using array syntax -- e.g.
If you are calling 4 on your data payload, then it won't make any difference to whether the payload is an array of arrays or an array of objects. The json string that is created will have identical syntax.You do not need to explicitly close the database connection after you are finished with the connection. When your script terminates, the connection will be closed for you automatically. Pembahasan tutorial kali ini merupakan kebalikan dari tutorial sebelumnya. Tutorial ini akan membahas bagaimana membuat script PHP untuk mengenerate data format JSON yang berasal dari database MySQL/MariaDB. Studi kasus yang akan dipilih, masih sama dengan sebelumnya yaitu menggunakan data kota-kota yang ada di Inggris. Misalkan kita memiliki sebuah tabel dengan nama ‘zip’ di MySQL/MariaDB sebagai berikut: Isi tabel data MySQL/MariaDB kota/daerah di InggrisDari data di atas, kita akan mengenerate data format JSON dengan struktur setiap record datanya sebagai berikut: Contoh format struktur JSON dari sebuah record data kota “AGAWAM”Ide dasar untuk mengenerate data JSON dari MySQL/MariaDB adalah sebagai berikut:
Dalam hal ini langkah 2, 3, dan 4 dilakukan terus menerus untuk semua record data yang terbaca dari database.
Implementasi dari ide dasar di atas ke dalam script PHP adalah seperti di bawah ini. <?php // parameter koneksi ke MySQL $dbhost = "localhost"; $dbuser = "..."; $dbpass = "..."; $dbname = "..."; // koneksi ke mysql $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); // query untuk membaca semua data record $query = "SELECT * FROM zip"; // jalankan query $result = mysqli_query($conn, $query); // proses looping untuk setiap data hasil query while ($data = mysqli_fetch_assoc($result)){ // membuat array asosiatif untuk setiap data $item = array("id" => $data['id'], "city" => $data['city'], "loc" => array(doubleval($data['lat']), doubleval($data['long'])), "pop" => intval($data['pop']), "state" => $data['state']); // proses encode array asosiatif ke json // lalu tampilkan echo json_encode($item)."\n"; } // tutup koneksi mysqli_close($conn); ?> Struktur array asosiatif yang menjadi acuan untuk membuat JSON adalah seperti berikut ini $item = array("id" => $data['id'], "city" => $data['city'], "loc" => array(doubleval($data['lat']), doubleval($data['long'])), "pop" => intval($data['pop']), "state" => $data['state']); Dalam hal ini, pemberian nama key dari array asosiatif tersebut nantinya akan menjadi key dari JSON nya. Khusus untuk key ‘loc’ (location), disini valuenya merupakan array yang elemennya merupakan gabungan dari latitude dan longitude. Adapun function Apabila script PHP di atas dijalankan, maka akan dihasilkan tampilan halaman berisi JSON seperti di bawah ini. |