PDO bind_param是未定义的方法

时间:2013-09-09 16:27:29

标签: php mysql pdo

我正在远离mysql和mysqli,因为stackoverflow上的许多用户都在不断地说出好消息。

我已经创建了一个数据库类并对其进行了测试,这可以很好地连接到数据库。我已经尝试更新我准备好的语句以匹配但是我处于不熟悉的领域并且最终收到以下错误:

Fatal error: Call to undefined method PDOStatement::bind_param() in E:\xampp\htdocs\imanage\insert.php on line 50

反映了这一行:

$stmt->bind_param("s", $_POST['email']);

另外关于这一点,我得到数据库连接成功和关闭语句返回给我以及致命错误,例如:

Successfully connected to the database!Successfully connected to the database!Successfully disconnected from the database!

我将解释我想要实现的目标:

  • 在注册用户
  • 之前检查数据库中是否存在电子邮件
  • 如果是这样告诉用户此电子邮件存在
  • 如果没有匹配,则将用户插入users表并加密密码

相关代码如下所示,如果有人能给我一些指导,我们将不胜感激。

的index.php

        <form id="loginForm" method="POST" action="class.Login.php">
        <input type="text" id="email" name="email" placeholder="E-mail">
        <input type="password" id="password" name="password" placeholder="Password" class="showpassword"> 
        <input type="submit" name="submit" value="Log in"></form>

insert.php

public function insert() {

                    $stmt = $this->pdo->prepare("SELECT COUNT(*) FROM users WHERE email=?");
                    $stmt->bind_param("s", $_POST['email']);
                    $stmt->execute();
                    $stmt->bind_result($email_count);
                    $stmt->fetch();//fecth
                    $stmt->close();     

                    if ($email_count > 0) {
                        echo "email exisits! click here to try <a href='register'>again</a>";
                        } else {
                            //escape the POST data for added protection
                            $username = isset($_POST['username']) ? $_POST['username'] : null;
                            $cryptedPassword = crypt($_POST['password']);
                            $password = $cryptedPassword;
                            $name = isset($_POST['name']) ? $_POST['name'] : null;
                            $email = isset($_POST['email']) ? $_POST['email'] : null;
                            $stmta = $this->pdo->prepare("INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, ?)");
                            //var_dump($this->pdo->error);
                            $stmta->bind_param('ssss', $username, $password, $name, $email); // bind strings to the paramater

                                /* execute prepared statement */
                                $stmta->execute();
                                printf("%d Row inserted.\n", $stmta->affected_rows);
                                /* close statement and connection */
                                $stmta->close();
                } // end email_count and insert to table
            } // end function

连接/ class.Database.php

<?php

// Database connection PDO

class Database {

    public function __construct() {
        // Connection information
        $host   = 'localhost';
        $dbname = 'imanage';
        $user   = 'root';
        $pass   = '';

        // Attempt DB connection
        try
        {
            $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo 'Successfully connected to the database!';
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

    }

     public function __destruct()
    {
        // Disconnect from DB
        $this->pdo = null;
        echo 'Successfully disconnected from the database!';
    }


}

$run = new Database();
?>

2 个答案:

答案 0 :(得分:6)

bind_param()设为bindParam()

答案 1 :(得分:4)

一些PDO示例

绑定参数示例

$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM users WHERE email=:email");
$stmt->bindParam(":email", $_POST['email']);
$stmt->execute();
$stmt->fetch(PDO::FETCH_ASSOC);

数组示例

$data = array($username, $password, $name, $email); 
$stmta = $this->pdo->prepare("INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, ?)");
$stmta->execute($data);

PDO tutorial