問題描述
我正在嘗試檢查電子郵件是否已在注冊中使用.我在學校做的時候還不錯,現在突然報錯:
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模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!