4 Replies - 379 Views - Last Post: 13 September 2017 - 02:37 AM Rate Topic: -----

#1 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 222
  • Joined: 02-February 15

PHP mysqli query returning blank results on a Select All ?

Posted 12 September 2017 - 03:15 AM

Hello,

I've ran into a bit of bother. I have a PHP function called 'displayMenuType' which is below:

public function displayMenuType() {

        $stmt = $this->conn->query("SELECT * FROM `Type`") or die($this->conn->error);
        //$result = $stmt->execute();

        if($stmt) {
            return  mysqli_fetch_all($stmt,MYSQLI_ASSOC);
        } else {
            return false;
        }
    }



This function looks at my Type table and returns all results (pretty simple). The Type table has 15 rows of data with 3 columns (ID, Type, Desc).

I call the PHP function in another class here:
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    require_once dirname(__FILE__) . '/DbOperation.php';
    $db = new DbOperation();

    if ($db->displayMenuType() != false) {
        echo json_encode($db->displayMenuType());
    } else {
        echo 'There was an error retrieving the menu';
    }
} else {
    echo 'You are not authorized';
}



For some reason when I execute, I get a blank page, no errors displayed and no results. If however I go back to the function and modify the SQL query to something like this... ("SELECT * FROM `Type` WHERE `ID` = 5"), and then execute, I get the expected result echo'd to my page. Can somebody tell me the problem with the Select * query ? It's been a while since I've done any extensive PHP or SQL work so I assume this is common knowledge.

Thanks,
Jlis

Is This A Good Question/Topic? 0
  • +

Replies To: PHP mysqli query returning blank results on a Select All ?

#2 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 222
  • Joined: 02-February 15

Re: PHP mysqli query returning blank results on a Select All ?

Posted 12 September 2017 - 06:02 AM

Although nobody replied, I always like to update my own threads when I find a solution...

Changed my code to this which prints any json_encode error messages and the problem was "Malformed UTF-8 characters, possibly incorrectly encoded"

Updated code:

f ($_SERVER['REQUEST_METHOD'] == 'GET') {
    require_once dirname(__FILE__) . '/DbOperation.php';
    $db = new DbOperation();

    if ($db->displayMenuType() != false) {
        $json = json_encode($db->displayMenuType());
        if($json) {
            echo $json;
        } else {
            echo json_last_error_msg();
        }
    } else {
        echo 'There was an error retrieving the menu';
    }
} else {
    echo 'You are not authorized';
}


Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,468
  • Joined: 08-August 08

Re: PHP mysqli query returning blank results on a Select All ?

Posted 12 September 2017 - 06:02 PM

Good that you've got it working, but when successful, you're executing the query twice. Better to do:
$queryResult = $db->displayMenuType();
if($queryResult != false) {
  $json = json_encode($queryResult);
  ...


Was This Post Helpful? 1
  • +
  • -

#4 no2pencil  Icon User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6548
  • View blog
  • Posts: 30,679
  • Joined: 10-May 07

Re: PHP mysqli query returning blank results on a Select All ?

Posted 12 September 2017 - 08:26 PM

Thank you for posting the solution, & good job on finding it.
Was This Post Helpful? 1
  • +
  • -

#5 jlis  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 222
  • Joined: 02-February 15

Re: PHP mysqli query returning blank results on a Select All ?

Posted 13 September 2017 - 02:37 AM

Thanks for the responses guys, just need to fix the Malformed UTF-8 characters error now. Also, thanks for spotting me running the query twice. Currently everything is hosted on a raspberry pi back at my office lol, so the less requests running on it the better ;)

Cheers,
Jlis
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1