skip to Main Content

MySQL query is only showing information if I have a condition in the SQL statement.

I’ve successfully used the SQL statement in phpmyadmin and it works great. I’ve changed the table name in the PHP code and it functions properly, the “people” table is the only one that causes a problem.

<?php
include 'dbconnectLocal.php';

$sql = "SELECT * FROM people WHERE nameFirst = 'Karen'";

$billings = array();

$billingResults = mysqli_query($connL, $sql);

while($row = mysqli_fetch_assoc($billingResults)){
    $billings[] = $row;
}

mysqli_close($connL);

$jsonOutput = json_encode($billings);

print("<pre>".json_encode($billings, JSON_PRETTY_PRINT)."</pre>");
?>

The above code produces the desired result, it gives me a JSON result of everyone whos name is Karen. But if I were to change it to $sql = “SELECT * FROM people” I get a blank screen.

2

Answers


  1. Chosen as BEST ANSWER

    After much digging around and frustration, it turned out to be a json_encode error. Specifically a UTF8 problem. Found this solution on php.net. I ran the array through this function before encoding it and it worked perfect.

    function utf8ize($d) {
    if (is_array($d)) {
    foreach ($d as $k => $v) {
    $d[$k] = utf8ize($v);
    }
    } else if (is_string ($d)) {
    return utf8_encode($d);
    }
    return $d;
    }
    

  2. Json have 4 mb limitation. You can make a var_dump of $billings to check what you get from db

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search