0

I am returning a JSON array from PHP and this is always empty. Is there something obvious I am missing?

<?php
    require("config.inc.php");
    $return_arr = array();

    $fetch = mysql_query("SELECT startingBool, endingBool FROM vote_count");

    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
        $row_array['startingBool'] = $row['startingBool'];
        $row_array['endingBool'] = $row['endingBool'];
        array_push($return_arr,$row_array);
    }

    echo json_encode($return_arr);

?>
6
  • 1
    you sure you have data in that table in the db? Commented Sep 22, 2014 at 21:43
  • Yes I am positive, I'm able to go into PHP myadmin and run that query and return the results I expect. Commented Sep 22, 2014 at 21:44
  • 1
    Have you tried simply while (...) { $return_arr[] = $row; } ? Commented Sep 22, 2014 at 21:45
  • 1
    what happens if you go var_dump($row); in that while block. What's the output? Commented Sep 22, 2014 at 21:45
  • 1
    what's the point of assigning values to $row_array rather than using $row directly. This just wastes memory. Have you verified that you enter into your while loop (i.e. have you done any basic debugging)? Commented Sep 22, 2014 at 21:47

3 Answers 3

3

I see absolutely no point in $row_array, and before you mess with json, check your dump:

<?php
require("config.inc.php");

$fetch = mysql_query("SELECT startingBool, endingBool FROM vote_count");

$return_arr = array();
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)){
    $return_arr[] = $row; 
}
var_dump($return_arr);
//echo json_encode($return_arr);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Var dump returns array(0) {}
0

Since you say using the phpmyadmin you can see that the vote_count table has data in it. The problem is probably in your config.inc.php (or where ever you setup the db connection)

Check if it's pointing to a different database or a server than the one you're viewing through phpmyadmin.

5 Comments

I'm using the same config.inc.php script for other queries but they are all updates to the database of the same table but those other queries work fine.
I'm using this generic script: pastebin.com/eM3VT4V6 with my information filled in at the top.
total | ID | lowThreshold | highThreshold | startingBool | endingBool
Your code should work. Smells like some configuration problem. I still doubt that code is using the same db as the phpmyadmin where you can see data in the vote_count
I directly included the connection in the page and im getting results now
0

Looks like the script is using PDO so it would be something like this:

$query = $db->query("SELECT startingBool, endingBool FROM vote_count");

$return_arr = array();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    $return_arr[] = $row; 
}
echo json_encode($return_arr);
?>

Comments

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.