<i id='1WvHk'><tr id='1WvHk'><dt id='1WvHk'><q id='1WvHk'><span id='1WvHk'><b id='1WvHk'><form id='1WvHk'><ins id='1WvHk'></ins><ul id='1WvHk'></ul><sub id='1WvHk'></sub></form><legend id='1WvHk'></legend><bdo id='1WvHk'><pre id='1WvHk'><center id='1WvHk'></center></pre></bdo></b><th id='1WvHk'></th></span></q></dt></tr></i><div id='1WvHk'><tfoot id='1WvHk'></tfoot><dl id='1WvHk'><fieldset id='1WvHk'></fieldset></dl></div>
    • <bdo id='1WvHk'></bdo><ul id='1WvHk'></ul>

    <small id='1WvHk'></small><noframes id='1WvHk'>

    <legend id='1WvHk'><style id='1WvHk'><dir id='1WvHk'><q id='1WvHk'></q></dir></style></legend><tfoot id='1WvHk'></tfoot>

      1. PDO 上 bind_result 的等价物是什么

        时间:2023-09-20

          <legend id='XOnE1'><style id='XOnE1'><dir id='XOnE1'><q id='XOnE1'></q></dir></style></legend>

            <tbody id='XOnE1'></tbody>

          <small id='XOnE1'></small><noframes id='XOnE1'>

              • <bdo id='XOnE1'></bdo><ul id='XOnE1'></ul>
                <i id='XOnE1'><tr id='XOnE1'><dt id='XOnE1'><q id='XOnE1'><span id='XOnE1'><b id='XOnE1'><form id='XOnE1'><ins id='XOnE1'></ins><ul id='XOnE1'></ul><sub id='XOnE1'></sub></form><legend id='XOnE1'></legend><bdo id='XOnE1'><pre id='XOnE1'><center id='XOnE1'></center></pre></bdo></b><th id='XOnE1'></th></span></q></dt></tr></i><div id='XOnE1'><tfoot id='XOnE1'></tfoot><dl id='XOnE1'><fieldset id='XOnE1'></fieldset></dl></div>
                <tfoot id='XOnE1'></tfoot>

                  本文介绍了PDO 上 bind_result 的等价物是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用准备好的语句转换为 PDO 和我,我想绑定我的结果 $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 与前面的示例相关.

                  I'm converting to PDO and Im using prepared statements, I want to bind my result as so $stmt->bind_result($email_count); so i am able to put this into an if statement to see if the e-mail exists however I am getting the error Fatal error: Call to undefined method PDOStatement::bind_result() in /Applications/XAMPP/xamppfiles/htdocs/imanage/insert.php on line 51 which relates to the previous example.

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

                  I'm guessing bind_result is not a PDO defined method, so is there an equivalent I could use?

                  我的代码如下,以防万一:

                  My code is below in case it helps:

                  插入.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.
                  ", $stmta->row_count);
                                                  /* close statement and connection */
                                                  $stmta->close();
                                  } // end email_count and insert to table
                              } // end function
                  
                        }
                  ?>
                  

                  connect/class.Database.php

                  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.

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

                  You do not need an ugly bind_result with PDO at all.

                  Yet you don't need to count either. Please, avoid unnecessary actions - they only bloat and obfuscate your code for no reason.

                  首先想想,你需要从查询中得到什么?你真的需要数数吗?不.你真正需要的只是一个标志 - 如果用户存在或不存在.因此,进行查询以返回这样的标志.

                  Think first, what you need from the query? Do you really need to count? No. What you actually need is just a flag - if user exists or no. So, make a query to return such a flag.

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

                  代码的所有其他部分也是如此

                  Same goes for all the other parts of code

                  //escape the POST data for added protection
                  

                  你实际上并没有逃避"此代码块中的任何数据且不添加任何保护.然而,我认为将 NULL 作为电子邮件插入绝对没有意义.你确定你真的想要吗?

                  You don't actually "escape" any data in this code block and add no protection. Yet I see absolutely no point in inserting NULL as email. Are you sure you really want it?

                  这篇关于PDO 上 bind_result 的等价物是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何确保 PDO 的 lastInsertId() 不是另一个同时插入 下一篇:PHP 将旧的 mysql_query 更改为 PDO

                  相关文章

                  最新文章

                  <legend id='L07pN'><style id='L07pN'><dir id='L07pN'><q id='L07pN'></q></dir></style></legend><tfoot id='L07pN'></tfoot>
                    <bdo id='L07pN'></bdo><ul id='L07pN'></ul>

                      <small id='L07pN'></small><noframes id='L07pN'>

                    1. <i id='L07pN'><tr id='L07pN'><dt id='L07pN'><q id='L07pN'><span id='L07pN'><b id='L07pN'><form id='L07pN'><ins id='L07pN'></ins><ul id='L07pN'></ul><sub id='L07pN'></sub></form><legend id='L07pN'></legend><bdo id='L07pN'><pre id='L07pN'><center id='L07pN'></center></pre></bdo></b><th id='L07pN'></th></span></q></dt></tr></i><div id='L07pN'><tfoot id='L07pN'></tfoot><dl id='L07pN'><fieldset id='L07pN'></fieldset></dl></div>