如何使用php中的预处理语句从mysql数据库中检索数据?

时间:2014-08-31 07:36:07

标签: php mysql prepared-statement

<?php
    require "config.php";
    if($stmt = $mysqli -> prepare("SELECT * FROM donorregistration WHERE Email=? and Password=?")) 
    {
        $email=$_POST['donorEmail'];
        $password=$_POST['password'];
        $stmt -> bind_param("ss",$email,$password);
        $stmt -> execute();
        $stmt->store_result();
        if($stmt->num_rows!=1)
        {
            echo "Sorry, Your E-mail ID or Password is incorrect";
        }
        else
        {

        }
    }
    $mysqli -> close();
?>

我不知道在else部分写什么。我想打印名为&#34; Account_Number&#34;的列。和列#34;名称&#34;来自mysql数据库。请帮帮我。

1 个答案:

答案 0 :(得分:0)

使用 fetch bind_result 语句检索数据

请尝试以下

<?php
require "config.php";
if($stmt = $mysqli -> prepare("SELECT * FROM donorregistration WHERE Email=? and Password=?")) 
{
    $email=$_POST['donorEmail'];
    $password=$_POST['password'];
    $stmt -> bind_param("ss",$email,$password);
    $stmt -> execute();
    $stmt->store_result();
    $stmt->bind_result($email,$pass);
    $stmt->fetch();
    if($stmt->num_rows!=1)
    {
        echo "Sorry, Your E-mail ID or Password is incorrect";
    }
    else
    {
        //retrieved 
       echo "Your email is $email and pass $pass");
    }
}
$mysqli -> close();

&GT;