致命错误在null上调用成员函数prepare() [英] Fatal error Call to a member function prepare() on null
问题描述
我正在尝试检查注册中是否已使用电子邮件.当我在学校工作时,它运行良好,但现在突然出现错误:
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(最少,我服务器上的那个)将不允许使用
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:
将 错误报告 添加到文件顶部,这将有助于发现错误.
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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!