简体   繁体   中英

Getting the ID,using a prepared statement

I'm trying to change some php/mysql code from 2008 so that it uses current standards. I'm fairly new to php/mysql so I'm struggling, despite doing plenty of research.

$sql_email_check = $db->prepare("SELECT `id` FROM `users` WHERE `email` = ? LIMIT 1");
$sql_email_check->bind_param('i', $email);
$sql_email_check->execute();

echo "<pre>".   print_r($sql_email_check, true)."<-- sql email check</pre>";

I'm trying to grab the ID so that I can use it at a later time, in my code.

My question is (I'm so confused, maybe I'm asking the wrong question), how do I display/grab the ID? I'm new to this so if I'm doing something, code wise, that isn't up to standards, please let me know.

Try this:

$sql_email_check = $db->prepare("SELECT `id` FROM `users` WHERE `email` = ? LIMIT 1");
$sql_email_check->bind_param('i', $email);
$sql_email_check->execute();
$sec = sql_email_check->get_result();
$row = $sec->fetch_assoc();

$id = $row['id'];    

echo $id;

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