Jump to content

Getting MySQLi result from bound statement


computernoob

Recommended Posts

So after running a statement, binding the parameters and then executing the statement, I have no clue how to catch the result. I know bind_result() will convert it to prepared variables or w/e they call it, but I want to get a hold of the actual value.

 

This is my current code:

$conn = connect();
$sql = "SELECT id, name FROM users WHERE email=? AND password=? AND role='admin'";
$stmt = $conn->prepare($sql) or die(mysqli_error($conn));
if($stmt){
    $stmt->bind_param("ss", $email, $password);
    $stmt->execute();
    $stmt->bind_result($id, $name);
    if($stmt->fetch()){
        // how do I catch $id and $name so I can do something like $_SESSION['id'] = $id
    }
}  

So essentially, $id and $name can only be used in a bound expression like printf("%s", $name) ... but how can I get $name to be the actual value so I can do something like $_SESSION['name'] = $name

 

Thanks for all the help in advance!

    /* fetch values */
    while ($stmt->fetch()) { //this is like " while($row = mysql_fetch_array($result)) {
        echo $id . " - " . $name . "<br>";
    }

    /* close statement */
    $stmt->close();

 

Copied from the manual, example 1

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.