什么是PDO上的bind_result

001221

我正在使用准备好的语句转换为PDO和Im,因此我想将结果绑定在一起,$stmt->bind_result($email_count);因此我可以将其放入if语句中,以查看电子邮件是否存在,但是我遇到了Fatal error: Call to undefined method PDOStatement::bind_result() in /Applications/XAMPP/xamppfiles/htdocs/imanage/insert.php on line 51与上一个相关的错误例。

我猜bind_result不是PDO定义的方法,所以有没有可以使用的等效方法?

我的代码在下面,以防万一:

insert.php

<?php

 include("connect/class.Database.php");

 class Users extends Database {

     public function insert() {

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

                    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;

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

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

      }
?>

connect / 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!';
    }


}

?>
你的常识

您根本不需要带有PDO的难看的bind_result。

然而,您也无需数数。请避免不必要的操作-它们只会无故膨胀和混淆您的代码。

首先考虑一下,查询需要什么?您真的需要数数吗?否。您实际需要的只是一个标志-用户是否存在。因此,进行查询以返回此类标志。

$stmt = $this->pdo->prepare("SELECT 1 FROM users WHERE email=?");
$stmt->execute(array($_POST['email']));
$exists = $stmt->fetchColumn();

其他所有代码也一样

//escape the POST data for added protection

您实际上并没有在此代码块中“转义”任何数据,也不添加任何保护。但是,我认为插入NULL作为电子邮件绝对没有意义。你确定你真的想要吗?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

mysqli bind_result并获取到PDO PostgreSQL

来自分类Dev

Mysqli bind_result错误

来自分类Dev

MYSQLi bind_result is returning null

来自分类Dev

mysqli bind_result()/只返回nul

来自分类Dev

bind_result不获取结果

来自分类Dev

bind_result mysqli无法正常工作

来自分类Dev

php mysqli bind_result返回1

来自分类Dev

为什么用bind_result重写代码后php api返回null?

来自分类Dev

stmt bind_result()不会绑定所有列

来自分类Dev

需要深入了解mysqli“ bind_result()”

来自分类Dev

PHP MySQLi bind_result是否为null?

来自分类Dev

如何查找bind_result()接收到的变量数

来自分类Dev

日期函数不适用于bind_result

来自分类Dev

bind_result() - 变量数与字段数不匹配

来自分类Dev

bind_result()和fetch()如何在范围之外更改变量?

来自分类Dev

创建SQL行之后,在PHP bind_result方法中获取有关该行的数据

来自分类Dev

如何存储MySQLi bind_result值以进行进一步处理?

来自分类Dev

mysqli_stmt :: bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配

来自分类Dev

错误:mysqli_stmt :: bind_result():绑定变量数与准备好的语句中的字段数不匹配

来自分类Dev

mysqli_stmt :: bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配

来自分类Dev

如何在PDO中转换mysqli_stmt_bind_result

来自分类Dev

在PHP PDO execute()上做什么会返回false?

来自分类Dev

在 tmux 上,“bind”和“bind-key”有什么区别?

来自分类Dev

PDO中的mysql_result

来自分类Dev

为什么UPDATE准备语句上的PDO :: fetchAll()导致“未缓冲的查询处于活动状态”异常?

来自分类Dev

PHP PDO bind_param错误

来自分类Dev

为什么我们在onClick事件上使用Bind方法

来自分类Dev

为什么会出现错误“车把错误:在对象上找不到属性'bind-attr'”

来自分类Dev

.bind()的用途是什么

Related 相关文章

  1. 1

    mysqli bind_result并获取到PDO PostgreSQL

  2. 2

    Mysqli bind_result错误

  3. 3

    MYSQLi bind_result is returning null

  4. 4

    mysqli bind_result()/只返回nul

  5. 5

    bind_result不获取结果

  6. 6

    bind_result mysqli无法正常工作

  7. 7

    php mysqli bind_result返回1

  8. 8

    为什么用bind_result重写代码后php api返回null?

  9. 9

    stmt bind_result()不会绑定所有列

  10. 10

    需要深入了解mysqli“ bind_result()”

  11. 11

    PHP MySQLi bind_result是否为null?

  12. 12

    如何查找bind_result()接收到的变量数

  13. 13

    日期函数不适用于bind_result

  14. 14

    bind_result() - 变量数与字段数不匹配

  15. 15

    bind_result()和fetch()如何在范围之外更改变量?

  16. 16

    创建SQL行之后,在PHP bind_result方法中获取有关该行的数据

  17. 17

    如何存储MySQLi bind_result值以进行进一步处理?

  18. 18

    mysqli_stmt :: bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配

  19. 19

    错误:mysqli_stmt :: bind_result():绑定变量数与准备好的语句中的字段数不匹配

  20. 20

    mysqli_stmt :: bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配

  21. 21

    如何在PDO中转换mysqli_stmt_bind_result

  22. 22

    在PHP PDO execute()上做什么会返回false?

  23. 23

    在 tmux 上,“bind”和“bind-key”有什么区别?

  24. 24

    PDO中的mysql_result

  25. 25

    为什么UPDATE准备语句上的PDO :: fetchAll()导致“未缓冲的查询处于活动状态”异常?

  26. 26

    PHP PDO bind_param错误

  27. 27

    为什么我们在onClick事件上使用Bind方法

  28. 28

    为什么会出现错误“车把错误:在对象上找不到属性'bind-attr'”

  29. 29

    .bind()的用途是什么

热门标签

归档