1

I am executing select query using mysqli ...

/** ------------  queries ----------      **/
        $stmt = $mysqli->prepare("SELECT * FROM dept");

        if(! $stmt)
        {
            echo "statement not prepared well";
        }
        else
        {
            echo $mysqli->error;
        }

        if (!$stmt->execute()) {
         echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }
 // add else
 else{
  echo "Query is successfully executed but no result fetch";
   }

        if (!($res = $stmt->get_result())) {
            echo "Getting result set failed: (" . $stmt->errno . ") " . $stmt->error;
        }
        /** ------------------------------- **/

        #------result ----
        var_dump($res->fetch_all());
        #---------(/result)----

My problem is the execute() is working fine but cannot fetch the records ... The table has good amount of data in it ... it is showing "Query is successfully executed but no result fetch" and after it Fatal error: Call to undefined method mysqli_stmt::get_result()

What am I doing wrong .. ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
alwaysLearn
  • 6,882
  • 7
  • 39
  • 67

2 Answers2

3

Break the following

if (!($res = $stmt->get_result())) {

to

$res = $stmt->get_result();
if( !$res ) {

Or rather to

$res = $stmt->get_result();
if( $res->num_rows == 0 ) {

The error(I didn't notice it earlier) is because

mysqli_stmt::get_result() is available only with mysqlnd.

You need to install/configure it following the guidelines here.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
0

What am I doing wrong .. ?

You are using mysqli instead of PDO

Whole your code could be rewritten in these few lines using PDO:

$stmt = $pdo->prepare("SELECT * FROM dept");
$stmt->execute();
var_dump($stmt->fetchall());

without all these numerous if and echo.
And the difference will be even bigger when you start with prepared statements

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345