我有一個腳本,執行mysql查詢,如果有行,它編碼消息。但我希望它能夠對消息和查詢的整個結果進行編碼。這是我的代碼使用方法:JSON編碼mysql查詢
<?php
include 'config.php';
// Connect to server and select databse.
mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
$id = @$_POST['id'];
// To protect MySQL injection (more detail about MySQL injection)
$id = stripslashes($id);
$id = mysql_real_escape_string($id);
$sql="Select * from table1 where ID='$id'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $id and $mypassword, table row must be 1 row
if ($count == 1) {
$message = array('status' => 'ok');
}
header('Content-Type: application/json');
print '{"key":'. json_encode($message) .'}';
?>
所以我想JSON的樣子:
{ "key": [ { "ID": "1", "NAME": "Test", "ADDRESS": "Test-street 123", "CONDITION": "false", "status": "ok" } ] }
所以我應該怎麼添加到$消息實現這一目標?
你不與結果做任何事情,只是檢索選定的行數。 – Gumbo