在mysqli中替换mysql_result

时间:2014-02-04 07:36:13

标签: php mysql mysqli

所以我正在将我的整个网站转换为mysqli_ *,并遇到了我无法找到替代品的第一个问题。基本上我有一个password_reset表单,只有具有链接的用户才能访问。一旦用户输入password_reset.php站点,他需要输入他的临时密码才能更改密码。我使用mysql_result来获取与通过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>';
}

我希望您能帮助找到一个易于实现的解决方案来替换mysql_results。正如您在上面的代码中看到的,我尝试使用不起作用的mysqli_result!

编辑:正如@randomizer建议的那样;我更新了我的代码并将mysql_result替换为:$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;

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您可以使用mysqli_query("yourquery")->fetch_object()->password;

另请查看准备好的语句,它将使您的工作更轻松: http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php