1

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.

5
  • just a side note: md5 is not secure for password hashing in a production environment!
    – Feek
    Mar 22, 2015 at 20:38
  • @Fred-ii- Enabling errors isnt the soulution Im looking for :), but my point is, how do I print the result, so I can process it next level Mar 22, 2015 at 20:41
  • Do you have anything stored in your db, far as a username/password goes? Mar 22, 2015 at 20:42
  • yes, I have info in database Mar 22, 2015 at 20:43
  • 1
    You've been given an "answer" below. Mar 22, 2015 at 20:44

1 Answer 1

1

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();
2
  • many thanks, is there a way to grab all the columns of the matching row into an array? just like the general mysql_fetch_assoc in mysql? Mar 22, 2015 at 20:44
  • 1
    I was already working on the edit for that - see the edited answer above. Mar 22, 2015 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.