简体   繁体   中英

How to fetch the result/row in mysqli

I am learning mysql but I am not sure what is next going to be here in this code to get the matching row as result, how can I fetch that?

$stmt= $db->prepare("SELECT * FROM users WHERE email=? and password = ?");
$stmt->bind_param("ss", $_POST['user_name'],md5($_POST['user_pass']));
$stmt->execute();

I am sure this is a simple one for you, please help me out in learning things.

Take a look at example #6 on this page

Here's the relevant bit, right after the bind_param() and the execute():

$out_id    = NULL;
$out_label = NULL;
if (!$stmt->bind_result($out_id, $out_label)) {
    echo "Binding output parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

while ($stmt->fetch()) {
    printf("id = %s (%s), label = %s (%s)\n", $out_id, gettype($out_id), $out_label, gettype($out_label));
}

Since I don't know what columns you are pulling in with your SELECT * I'll just leave the example as it stands.

You can also use fetch_assoc() which is probably a little easier to use and ends up being used a lot more commonly. Digging from the same page on php.net we get this relevant portion from example #8 (actually I had to modify it somewhat - example #8 is doing some funky stuff with buffering):

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

while ($row = $res->fetch_assoc()) {
    var_dump($row);
}
$res->close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM