php - Fatal error Call to a member function prepare() on null -
i trying check if email used in registration. worked when working on in school shows error:
fatal error: call member function prepare() on null
i use 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 registrace email = :email"); $sthandler->bindparam(':email', $_session['email']); $sthandler->execute(); if(filter_var($_session['email'], filter_validate_email)) { if($sthandler->rowcount() > 0){ echo "email used";}
edit: (i figured out).
i figured out why code not working , original answer no longer applies, have stricken out.
the reason why connection not work, because left out dbpass constant 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 may not have password set it, still required part of parameters.
original answer, no longer applies. (see edit above).
tbh, wasn't able reproduce error, amidst great effort.
however; after tinkering this, have discovered pdo (least, 1 on server), won't allow constants used first 2 parameters in dsn.
sidenote: if worked @ school, there may setting used don't know about.
you can however, assign variables constants, use variables in 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 top of file(s) find errors.
<?php error_reporting(e_all); ini_set('display_errors', 1); // rest of code sidenote: error reporting should done in staging, , never production.
Comments
Post a Comment