I have a PHP file which currently returns JSON in these two ways:
If an error happens, I do this:
$post_data = array('error' => "no_member_id");
echo json_encode($post_data);
and if there is no error, and I need to return data in JSON format, I do this:
if (mysql_num_rows($result) > 0 )
{
$rows = array();
while($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
}
echo json_encode($rows);
}
But what I really need to do is return the data in a format like this:
{"result":"ok", data :[{"data1":"value1", "data2":"value2"}]}
or this:
{"result":"error", data :[{"error":"no_id"}]}
Could someone please help me understand how to do that?
Thanks!!
mysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.E_DEPRECATEDnotice.