Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:

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

I use this to include

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"
) );

In here

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";}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
376 views
Welcome To Ask or Share your Answers For Others

1 Answer

Edit: (I figured it out).

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

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"
) );

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

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

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.

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);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...