John Davidson

php - Call to undefined method mysqli_stmt::get_result

0 comments
Message:


Here's my code:



include 'conn.php';
$conn = new Connection();
$query = 'SELECT EmailVerified, Blocked FROM users WHERE Email = ? AND SLA = ? AND `Password` = ?';
$stmt = $conn->mysqli->prepare($query);
$stmt->bind_param('sss', $_POST['EmailID'], $_POST['SLA'], $_POST['Password']);
$stmt->execute();
$result = $stmt->get_result();


I get the error on last line as: Call to undefined method mysqli_stmt::get_result()



Here is the code for conn.php:



define('SERVER', 'localhost');
define('USER', 'root');
define('PASS', 'xxxx');
define('DB', 'xxxx');
class Connection{
/**
* @var Resource
*/
var $mysqli = null;

function __construct(){
try{
if(!$this->mysqli){
$this->mysqli = new MySQLi(SERVER, USER, PASS, DB);
if(!$this->mysqli)
throw new Exception('Could not create connection using MySQLi', 'NO_CONNECTION');
}
}
catch(Exception $ex){
echo "ERROR: ".$e->getMessage();
}
}
}


If I write this line:



if(!stmt) echo 'Statement prepared'; else echo 'Statement NOT prepared';


It prints 'Statement NOT prepared'. If I run the query directly in the IDE replacing ? marks with values, it works fine. Please note that $conn object works fine in other queries in the project.



Any help please.......



My Answer:
> It seems like you are trying to use the `get_result()` method on a `mysqli_stmt` object, but this method is not available in the `mysqli_stmt` class.

To fetch the result set from a prepared statement in PHP, you can use the `mysqli_stmt::bind_result()` method to bind variables to the result set columns and then fetch the results using `mysqli_stmt::fetch()` method.

Here's an example of how you can fetch the result set from a prepared statement in PHP:

php
include 'conn.php';
$conn = new Connection();
$query = 'SELECT EmailVerified, Blocked FROM users WHERE Email = ? AND SLA = ? AND `Password` = ?';
$stmt = $conn->prepare($query);

// Bind parameters
$stmt->bind_param('sss', $email, $sla, $password);

$email = '[email protected]';
$sla = 'example_sla';
$password = 'example_password';

// Execute the query
$stmt->execute();

// Bind result variables
$stmt->bind_result($emailVerified, $blocked);

// Fetch the results
$stmt->fetch();

// Now you can use the $emailVerified and $blocked variables to access the fetched values
echo $emailVerified;
echo $blocked;

// Close the statement
$stmt->close();


In this example, we first prepare the query using the `prepare()` method, bind the parameters using `bind_param()`, execute the query using `execute()`, bind the result variables using `bind_result()`, fetch the results using `fetch()`, and finally close the statement using `close()`.

Rate this post

3 of 5 based on 4354 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us