• <bdo id='SnpBQ'></bdo><ul id='SnpBQ'></ul>
  • <tfoot id='SnpBQ'></tfoot>
    <legend id='SnpBQ'><style id='SnpBQ'><dir id='SnpBQ'><q id='SnpBQ'></q></dir></style></legend>
  • <small id='SnpBQ'></small><noframes id='SnpBQ'>

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

        致命错误 在 null 上调用成员函数 prepare()

        时间:2023-09-20
        <i id='YyYWY'><tr id='YyYWY'><dt id='YyYWY'><q id='YyYWY'><span id='YyYWY'><b id='YyYWY'><form id='YyYWY'><ins id='YyYWY'></ins><ul id='YyYWY'></ul><sub id='YyYWY'></sub></form><legend id='YyYWY'></legend><bdo id='YyYWY'><pre id='YyYWY'><center id='YyYWY'></center></pre></bdo></b><th id='YyYWY'></th></span></q></dt></tr></i><div id='YyYWY'><tfoot id='YyYWY'></tfoot><dl id='YyYWY'><fieldset id='YyYWY'></fieldset></dl></div>

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

          <bdo id='YyYWY'></bdo><ul id='YyYWY'></ul>

        • <legend id='YyYWY'><style id='YyYWY'><dir id='YyYWY'><q id='YyYWY'></q></dir></style></legend>

            <tfoot id='YyYWY'></tfoot>

              <tbody id='YyYWY'></tbody>
                  本文介绍了致命错误 在 null 上调用成员函数 prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试检查电子邮件是否已在注册中使用.我在学校做的时候还不错,现在突然报错:

                  I am trying to check if email was already used in Registration. It worked well when I was working on it in the school but now it suddenly shows an error:

                  致命错误:在 null 上调用成员函数 prepare()

                  Fatal error: Call to a member function prepare() on null

                  我用它来包含

                  define("dbserver", "localhost");
                  define("dbuser", "user");
                  define("dbpass", "");
                  define("dbname", "user");    
                  
                  
                  $db = new PDO(
                  "mysql:host=" .dbserver. ";dbname=" .dbname,dbuser,
                  array(
                  PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
                  PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
                  ) );
                  

                  在这里

                  session_start();
                  include "DBjoin.php";
                  if(isset($_POST["email"])) {
                    $_SESSION['email'] = $_POST["email"];
                    }   
                  if(isset($_POST["nick"])) {
                    $_SESSION['nick'] = $_POST["nick"];         
                  }    
                  if(isset($_POST["pass"])) {
                    $_SESSION['pass'] = $_POST["pass"];
                    $_SESSION['pass'] = base64_encode($_SESSION['pass']);    
                  }
                  $sthandler = $db->prepare("SELECT Email FROM Registrace WHERE Email =     :email");
                  $sthandler->bindParam(':email', $_SESSION['email']);
                  $sthandler->execute();               
                  if(filter_var($_SESSION['email'], FILTER_VALIDATE_EMAIL)) {
                  if($sthandler->rowCount() > 0){
                        echo "Email is used";}
                  

                  推荐答案

                  (我想通了).

                  我终于弄清楚为什么你的代码不起作用,我原来的答案不再适用,我已经解决了.

                  I finally figured out why your code is not working and my original answer no longer applies, which I have stricken out.

                  您的连接不起作用的原因是因为您从连接参数中遗漏了 dbpass 常量.

                  The reason why your connection does not work, is because you left out the dbpass constant from the connection parameter.

                  $db = new PDO(
                  "mysql:host=" .dbserver. ";dbname=" .dbname,dbuser,dbpass,
                  array(
                  PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
                  PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
                  ) );
                  

                  • 即使您可能没有为其设置密码,它仍然需要成为参数的一部分.

                    • Even though you may not have a password set for it, it is still required to be part of the parameters.

                      http://php.net/manual/en/ref.pdo-mysql.connection.php

                      原始答案,不再适用.(见上面的编辑).

                      Original answer, which no longer applies. (see edit above).

                      <打击>TBH,经过一番努力,我还是无法重现该错误.

                      TBH, I wasn't able to reproduce the error, amidst a great effort.

                      然而;对此进行修补后,发现 PDO(至少,我服务器上的那个)不允许 constants 用作 DSN 中的前 2 个参数.

                      However; after tinkering with this, have discovered that PDO (least, the one on my server), won't allow for constants be used as the first 2 parameters in the DSN.

                      旁注:如果您所说的在您的学校有效,那么可能使用了我不知道的设置.

                      Sidenote: If what you say worked at your school, then there may be a setting used that I don't know about.

                      但是,您可以将变量分配给常量,然后在 DSN 中使用这些变量.

                      You can however, assign variables to the constants, then use those variables in the DSN.

                      $servername = "localhost";
                      $dbname = "user";
                      
                      define("dbuser", "user");
                      define("dbpass", "");
                      
                      $dsn = "mysql:host=$servername;dbname=$dbname";
                      
                      $username = dbuser; // variable equals constant
                      $password = dbpass; // variable equals constant
                      
                      $options = array(
                      PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
                      PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
                      ); 
                      
                      $db = new PDO($dsn, $username, $password, $options);
                      

                      有关 PDO 连接的更多信息,请访问:

                      For more information on PDO connection, visit:

                      • http://php.net/manual/en/ref.pdo-mysql.connection.php

                      错误报告添加到将有助于查找错误的文件顶部.

                      Add error reporting to the top of your file(s) which will help find errors.

                      <?php 
                      error_reporting(E_ALL);
                      ini_set('display_errors', 1);
                      
                      // rest of your code
                      

                      旁注:错误报告只应在暂存阶段进行,切勿在生产中进行.

                      Sidenote: Error reporting should only be done in staging, and never production.

                      这篇关于致命错误 在 null 上调用成员函数 prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 PDO 准备语句中使用 LIKE 子句? 下一篇:在 PHP PDO MYSQL 中插入多行的最佳方法是什么?

                  相关文章

                  最新文章

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

                  1. <tfoot id='5IJDB'></tfoot>

                    <legend id='5IJDB'><style id='5IJDB'><dir id='5IJDB'><q id='5IJDB'></q></dir></style></legend>

                      <small id='5IJDB'></small><noframes id='5IJDB'>