0

So I have this PHP script that let me get photo objects from MySQL database, while fetching the results with mysql_fetch_array() function I push the row into an array. Which works, simple and good.

After the while, I do an echo of the array size and it does work too. Then, when I try to encode the array to json format and I test it I get "Response does not contain any data" with a Ok status from Chrome's Advanced Rest Client.

if (mysql_num_rows($result) > 0) 
{
// looping through all results
// photo node
$response["photos"] = array();

while ($row = mysql_fetch_array($result)) {
    // temp photo array
    $photo = array();

    $photo["photoid"] = $row["photoid"];
    $photo["photodescription"] = $row["photodescription"];
    $photo["uploaderid"] = $row["uploaderid"];
    $photo["takenat"] = $row["takenat"];
    $photo["nblikes"] = $row["nblikes"];
    $photo["photourl"] = $row["photourl"];
    $photo["thumbnailurl"] = $row["thumbnailurl"];


    // push single photo into final response array
    array_push($response["photos"], $photo);
}
// success
$response["success"] = 1;

// echoing JSON response
echo sizeof($response["photos"]);
echo json_encode($response);
}

Can anyone help, please ?

13
  • Where do you see that error message? Are you returning this data to Javascript? Could be facing CURSE of the CORS Commented May 28, 2015 at 15:36
  • What do you see in red block of this link ? php.net/manual/en/function.mysql-fetch-array.php Commented May 28, 2015 at 15:38
  • what does var_dump($response); give you? Commented May 28, 2015 at 15:39
  • @Hanky웃Panky that message is the result that the Advanced Rest Client App for Chrome gives me. Commented May 28, 2015 at 15:42
  • @zlen, I do know that, but I have written many other scripts which works fine even with that. Commented May 28, 2015 at 15:43

2 Answers 2

1

Thanks to RonnySkansing, it appeared that I had an error while encoding. Which is a "JSON_ERROR_UTF8: Malformed UTF-8 characters, possibly incorrectly encoded"

I've added then this : mysql_set_charset("utf8"); And it's done. Voila.

Sign up to request clarification or add additional context in comments.

Comments

0

Your select probably has an error or the number of rows is 0. Try printing the numbers of rows.

echo mysql_num_rows($result);

2 Comments

Nope, tried the request on MySQL and it works fine. Also the var_dump shows that it works.
Size of will always print something so it can't be hitting that line if you're not getting a response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.