0

I am writing a code for live searching a record with query passed by user. The following is code written:

if(isset($_REQUEST["term"])){
    $sql = "SELECT * FROM students WHERE rollno LIKE ?";
    if($stmt = $conn->prepare($sql)){
        $stmt->bind_param("s", $param_term);
        $param_term = $_REQUEST["term"] . '%';
        if($stmt->execute()){
            $result = $stmt->get_result(); // error on this line
            if($result->num_rows > 0){
                // Fetch result rows as an associative array
                while($row = $result->fetch_array(MYSQLI_ASSOC)){
                    $rollno = $row['rollno'];
                    $name = $row['name'];
                    $image = $row['image'];
                    echo $rollno." ".$name." ".$image;
                }
            }
        }
    }
}

the code works fine on thew local server. But the same code does not work on the live server with same database. I am getting an error Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/u687417727/public_html/digiclass/panel/backend-search.php on line 25 right after I enter some query. I have shown the error in the code with comment. What could be the problem? Help please. Trying to evaluate it from long time.

2 Answers2

0

You have two options either you install mysqlnd on your server in php_info How to enable mysqlnd for php?

or write your own function for that

 function get_result($stmt){
        $stmt->execute(); //Execute query
        $stmt->store_result(); //Store the results
        $num_rows = $stmt->num_rows; //Get the number of results
        $results = NULL;
        if($num_rows > 0){
            //Get metadata about the results
            $meta = $stmt->result_metadata();
            //Here we get all the column/field names and create the binding code
            $bind_code = "return mysqli_stmt_bind_result(\$stmt, ";
            while($_field = $meta->fetch_field()){
                $bind_code .= "\$row[\"".$_field->name."\"], ";
            }
            //Replace trailing ", " with ");"
            $bind_code = substr_replace($bind_code,");", -2);
           //Run the code, if it doesn't work return NULL
            if(!eval($bind_code)) {
                $stmt->close();
                return NULL;
            }
            //This is where we create the object and add it to our final result array
            for($i=0;$i<$num_rows;$i++){
               //Gets the row by index
                $stmt->data_seek($i);
                //Update bound variables used in $bind_code with new row values
                $stmt->fetch();
                foreach($row as $key=>$value){
                    //Create array using the column/field name as the index
                    $_result[$key] = $value;
                }
                //Cast $_result to object and append to our final results
                $results[$i] = (object)$_result;
            }
        }
        $stmt->close();
        return $results;
    }
DEEPAK
  • 1,364
  • 10
  • 24
0

I RESOLVED this issue.

I was also frustrated in this issue but after huge struggling and surfing I found solution for this issue. If get_result() not working with "mysqli" extension in your cpnael then follow these :

I simply resolved this issue by the way of I just DISABLED "mysqli" and ENABLED "nd_mysqli" extension in my Cpanel. Now get_result() statements working and resolved this big head ache issue.