Postingan lainnya
Buku Ini Koding!
Baru!
Buku ini akan jadi teman perjalanan kamu belajar sampai dapat kerjaan di dunia programming!
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
cetak hasil json
gan mau tanya gimana caranya cetak hasil json ini kedalam tabel
{"code":"00","desc":"Transaksi gagal.","data":{"nomor_handphone":"4123123","nama":"jhon","mata_uang":"IDR","jumlah_setoran":20000,"admin":0,"total_pembayaran":20000,"nomor_referensi":"12312321"}}
0
1 Jawaban:
Kamu bisa gunakan json_decode :
/* create a connection */
$mysqli = new mysqli("localhost", "root", null, "yourDatabase");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* let's say we're grabbing this from an HTTP GET or HTTP POST variable called jsonGiven... */
$jsonString = $_REQUEST['jsonGiven'];
/* but for the sake of an example let's just set the string here */
$jsonString = '{"name":"jack","school":"colorado state","city":"NJ","id":null}
';
/* use json_decode to create an array from json */
$jsonArray = json_decode($jsonString, true);
/* create a prepared statement */
if ($stmt = $mysqli->prepare('INSERT INTO test131 (name, school, city, id) VALUES (?,?,?,?)')) {
/* bind parameters for markers */
$stmt->bind_param("ssss", $jsonArray['name'], $jsonArray['school'], $jsonArray['city'], $jsonArray['id']);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
source: [link]https://stackoverflow.com/questions/11320796/saving-json-string-to-mysql-database [/link] https://stackoverflow.com/questions/12261531/php-variable-to-json-object http://php.net/manual/en/function.json-decode.php
1