Home > Software design >  mysqli_query Returning False, mysqli_report won't work either [duplicate]
mysqli_query Returning False, mysqli_report won't work either [duplicate]

Time:09-28

I am brand new to PHP in general and I'm trying to print information from a MySQL database. However, the following is printing the "Could not get data: " but won't print an error code. I can't get it to recognize mysqli_report()MYSQLI_REPORT_STRICT);

The main issue is that I can't get mysqli_query to work. I have tried both that one and mysqli_real_query but both have returned false. I have looked at the documentation for it as well but can't figure out what I'm doing wrong here.

The database and table function the way they should, I checked using MySQL in terminal which printed the appropriate table. Not sure if this will help, but it's in a LAMP server on a Raspberry Pi.

The connection details were removed for security although you would literally just be getting access to an empty raspberry pi lol. Thanks!

P.S. I hope I am formatting things correctly for stackoverflow, I'm pretty new around this too.

<?php
   $dbhost = '';
   $dbuser = '';
   $dbpass = '';

   mysqli_report(MYSQLI_REPORT_STRICT);
   
   $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn ) {
      die('Could not connect: ' . mysqli_error());
   }
   
   $sql = 'SELECT name, price FROM data';
   mysqli_select_db('phpexample');
   $result = mysqli_query( $conn, $sql ); // THIS IS WHERE ITS FAILING
   
   if(! $result ) {
      die('Could not get data: ' . mysqli_error());
   }
   
   while($row = mysqli_fetch_array($retval, MYSQL_NUM)) {
      echo "name :{$row[0]}  <br> ".
         "price : {$row[1]} <br> ".
         "--------------------------------<br>";
   }
   
   echo "Fetched data successfully\n";
   
   mysqli_close($conn);
?>

CodePudding user response:

You're missing the $conn error to mysqli_select_db():

mysqli_select_db($conn, 'phpexample');

mysqli_error($conn) should have returned "No database selected", I don't know why you didn't see that when you corrected mysqli_error() to mysqli_error($conn). You also should have gotten an error due to not providing enough arguments to mysqli_select_db().

You can also specify the database as an argument to mysqli_connect().

My guess is you converted this code from mysql_XXX. All the corresponding mysqli_ functions require a $conn argument, except for the ones that fetch from a result object.

<?php
   $dbhost = '';
   $dbuser = '';
   $dbpass = '';

   mysqli_report(MYSQLI_REPORT_STRICT);
   
   $conn = mysqli_connect($dbhost, $dbuser, $dbpass, 'phpexample');
   
   if(! $conn ) {
      die('Could not connect: ' . mysqli_select_error());
   }
   
   $sql = 'SELECT name, price FROM data';
   $result = mysqli_query( $conn, $sql ); // THIS IS WHERE ITS FAILING
   
   if(! $result ) {
      die('Could not get data: ' . mysqli_error($conn));
   }
   
   while($row = mysqli_fetch_array($retval, MYSQL_NUM)) {
      echo "name :{$row[0]}  <br> ".
         "price : {$row[1]} <br> ".
         "--------------------------------<br>";
   }
   
   echo "Fetched data successfully\n";
   
   mysqli_close($conn);
?>
  • Related