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

Fatal error Call to a member function prepare() on null(致命错误 在 null 上调用成员函数 prepare())
本文介绍了致命错误 在 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

      本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)