I create a json output, but got these errors, Any ideas ?!
Code 1:
public function getChatRoomData($uid, $name, $mess, $_roomID)
{
mysql_query("INSERT INTO _roomData(uid, userName, messageText, roomID, postedAt) VALUES('$uid', '$name', '$mess', '$_roomID', NOW())");
return mysql_query("SELECT * FROM _roomData WHERE roomID = '$_roomID';");
}
Code 2:
if ($tag == 'roomEnter') {
$userID = $_POST['_uniqeID'];
$userName = $_POST['_name'];
$userMessage = $_POST['_message'];
$roomID = $_POST['_roomID'];
$queryFunctions = $db->getChatRoomData($userID , $userName, $userMessage, $roomID);
$rows = array();
while($result = mysql_fetch_assoc($queryFunctions))
{
$rows["uid"] = $result["uid"];
$rows["name"] = $result["userName"];
$rows["roomDetails"]["message"] = $result["messageText"];
}
echo json_encode($rows);
}
Logcat Error :
07-30 18:59:46.843: E/JSON(26633): <br />
07-30 18:59:46.843: E/JSON(26633): <b>Parse error</b>: syntax error, unexpected '}' in <b>/home/merlinga/public_html/_mess/index.php</b> on line <b>106</b><br />
07-30 18:59:46.944: E/JSON Parser(26633): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
07-30 19:02:30.293: E/JSON(26633): <br />
07-30 19:02:30.293: E/JSON(26633): <b>Parse error</b>: syntax error, unexpected '}' in <b>/home/merlinga/public_html/_mess/index.php</b> on line <b>106</b><br />
07-30 19:02:30.293: E/JSON Parser(26633): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
There's another error in here. You are assigning $rows['..'] = $results['xxx']
in every iteration of the loop. You should be using $rows[] = xxx and add a new array or object to the rows array for every iteration.
Fix the first syntax error you are receiving and then use something like this for the other error:
$rows = array();
while($result = mysql_fetch_assoc($queryFunctions))
{
$row = array();
$row['uid'] = $result['uid'];
$row['name'] = $result['userName'];
// or just $rows[] = $result;
// see below.
$row["roomDetails"] = array();
$row["roomDetails"]["message"] = $result["messageText"];
$rows[] = $row;
}
echo json_encode($rows);
Reasons for and against using $rows[] = $results
instead of mapping the results to a new array or object:
Pros:
Cons
Same as above. New attributes/columns that you don't want to expose may leak and cause bigger HTTP responses than necessary or even worse: a security risk.
You might want to use this layer to map the table columns to a view model with differently named and aggregated attributes
You have a syntax error in your PHP code, which results in an error message that is clearly not JSON output, resulting in more errors when you try to treat it as JSON.
Check your PHP code, specifically around line 106, and see what's causing the issue.