Monday 3 September 2018

Mysqli query does not retrieve any records

I am trying to run a PHP script that uses mysqli library, but it fetches no results on the webhosting server while in my machine running WLAMP PHP 5.14 it works well.

Can you please tell me if the problem is related with mysqli and if there is a way to make it work for my case?.
The lines I am looking to make work look like this:
$sql = "call sp_give_me_data(null,null,null);";

if (!$mysqli->query($sql)) {
   die('Invalid query: ' . mysql_error());
} else {

   $result = $mysqli->query($sql);

   if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {
       $datainfo[] = $row;
     }
     $output = json_encode($datainfo);
     echo utf8_encode($output);
   } else {
     echo "0";
   }

}

as I said it fetches "0" records on the hosting which supports PHP 5.3-5.5 and it fetches the correct number of records on my machine. The connection to the remote database (which is the same in both cases) seems successful as well.
Thanks for the insight.
Update
I changed my code so now I am sending the query just once:
mysqli_set_charset($mysqli,"utf8"); //utf-8 accents query
$result = $mysqli->query($sql);     

if (!$result) {
    die('Invalid query: ' . mysql_error());
} else {

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $datainfo[] = $row;
        }
        //utf-8 accents unescaping to be shown correctly
        $output = json_encode($datainfo,JSON_UNESCAPED_UNICODE);
        echo $output;                       

    } else {
        echo "0";
    }
}

0 comments:

Post a Comment