I have a script that does a mysql query and if there are rows it encodes a message. But I want it to encode the message and the whole result of the query. This is the code I use:
<?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) .'}';
?>
So I want the JSON to look like:
{
"key": [
{
"ID": "1",
"NAME": "Test",
"ADDRESS": "Test-street 123",
"CONDITION": "false",
"status": "ok"
}
]
}
So what should I add to $message to achieve this?