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

时间:2023-04-08
本文介绍了致命错误 在 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文章