简体   繁体   中英

mysql_result replacement in mysqli

So I'm in the process of transitioning my entire site to mysqli_* and came across my first problem which I cannot find a replacement for. Basically I have a password_reset form that can only be accessed by a user with a link. Once the user enters the password_reset.php site, he needs to enter his temporary password before he can change his password. I use mysql_result to get the temporary password that is associated to the username entered via http POST.

$link_users = mysqli_connect("$host", "$username", "$password", "$db_name") or die("cannot connect");
$results=mysqli_query($link_users, "SELECT temporary_password FROM $tbl_name");
$row = mysqli_fetch_array($results);


$new_pass1 = $_POST['new1_password'];
$new_pass2 = $_POST['new2_password'];

$new_encrypted_mypassword = sha1($new_pass1);

$temp_pass = $_POST['temp_password'];
$username_fetch = $_POST['temp_username'];
$temppass_database = mysqli_result(mysqli_query($link_users, "SELECT temporary_password FROM $tbl_name WHERE temporary_password='$temp_pass' AND username='$username_fetch'"), 0);

if ($temp_pass == $temppass_database && $new_pass1 == $new_pass2){
mysqli_query($link_users, "UPDATE $tbl_name SET password='$new_encrypted_mypassword' WHERE username='$username_fetch'");
}

elseif ($temp_pass != $temppass_database){
echo '<div class="alert">Invalid Temporary Password or Username!</div>';
}

// Delete temporary password
if ($temp_pass == $temppass_database && $new_pass1 == $new_pass2 ) {
mysqli_query($link_users, "UPDATE $tbl_name set temporary_password = null WHERE username='$username_fetch'");
echo '<div class="alert">Updated Password!</div>';
}

I hope you can help find an easy to implement solution to replace mysql_results. As you can see in my code above, I tried to use mysqli_result which does not work!

EDIT: As @randomizer suggested; I updated my code and replaced mysql_result with: $temppass_database = mysqli_query($link_users, "SELECT temporary_password FROM $tbl_name WHERE temporary_password='$temp_pass' AND username='$username_fetch'")->fetch_object()->temporary_password;

Thanks for the help!

You can use mysqli_query("yourquery")->fetch_object()->password;

Also have a look into prepared statements, it will make your work much easier: http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

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