guys if i change my hosting i get a error in my local ampps host i not have not one problem now i see this error everithing is same php version is 7.1 and in local is 7.1 but why i get this error?
This is error code :
Fatal error: Uncaught Error: Call to a member function userDetails() on null in /home/root/requests/index.php:14 Stack trace: #0 {main} thrown in /home/root/requests/index.php on line 14
this is my function in top of index.php
<?php
include('config.php');
include('session.php');
$userDetails=$userClass->userDetails($session_uid);
this is my session.php code
<?php
if(!empty($_SESSION['uid']))
{
$session_uid=$_SESSION['uid'];
include('userClass.php');
$userClass = new userClass();
}
if(empty($session_uid))
{
$url=BASE_URL.'/login.php';
header("Location: $url");
}
?>
this is my userClass.php code :
<?php
class userClass
{
/* User Login */
public function userLogin($usernameEmail,$password)
{
try{
$db = getDB();
$hash_password= hash('sha256', $password); //Password encryption
$stmt = $db->prepare("SELECT uid FROM users WHERE (username=:usernameEmail or email=:usernameEmail) AND password=:hash_password");
$stmt->bindParam("usernameEmail", $usernameEmail,PDO::PARAM_STR) ;
$stmt->bindParam("hash_password", $hash_password,PDO::PARAM_STR) ;
$stmt->execute();
$count=$stmt->rowCount();
$data=$stmt->fetch(PDO::FETCH_OBJ);
$db = null;
if($count)
{
$_SESSION['uid']=$data->uid; // Storing user session value
return true;
}
else
{
return false;
}
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
/* User Details */
public function userDetails($uid)
{
try{
$db = getDB();
$stmt = $db->prepare("SELECT email,username,name FROM users WHERE uid=:uid");
$stmt->bindParam("uid", $uid,PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_OBJ); //User data
return $data;
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
}
?>
Did you defined $userClass, if not then you should make an object the userClass that you are using here then call the method inside that class and it will work.
Related
This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 27 days ago.
On logging in, This error pops up.
Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\twitter\core\classes\user.php:19 Stack trace: #0 C:\xampp\htdocs\twitter\includes\login.php(17): User->login('user#email.com', 'passwordofuser') #1 C:\xampp\htdocs\twitter\index.php(59): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\twitter\core\classes\user.php on line 19
I'm a beginner in PHP and was trying to validate the "entered" email and password by connecting it to SQL DATABASE. I wanted to display an "error" message if the email or password is not available in database.
This is user.php.
The error is at line 19.
$stmt = $this->pdo->prepare("SELECT `user_id` FROM `users` WHERE `email` = :email AND `password` = :password");
user.php
<?php
class User
{
protected $pdo;
function _construct($pdo)
{
$this->pdo = $pdo;
}
public function checkInput($var)
{
$var = htmlspecialchars($var);
$var = trim($var);
$var = stripcslashes($var);
return $var;
}
public function login($email, $password)
{
$stmt = $this->pdo->prepare("SELECT `user_id` FROM `users` WHERE `email` = :email AND `password` = :password");
$stmt->bindParam(":email", $email, PDO::PARAM_STR);
$stmt->bindParam(":password",md5($password), PDO::PARAM_STR);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_OBJ);
$count = $stmt->rowCount();
if($count > 0)
{
$_SESSION['user_id'] = $user->user_id;
header('Location: home.php');
}
else
{
return false;
}
}
}
?>
Now, here is my connection.php
<?php
$dsn = 'mysql:host=localhost; dbname=tweety';
$user = 'root';
$pass = '';
try
{
$pdo = new PDO($dsn, $user, $pass);
}
catch(PDOException $e)
{
echo 'Connnection error! ' . $e->getMessage();
}
?>
Here is login.php
I know that this question is asked previously but i can't help myself find a solution.Any help would be appreciated.
WHAT I TRIED:
Tried checking for typo error in connection.php
Tried restarting my SQL server from XAMPP.
Tried referring other "stackoverflow" questions.
In user.php
Rename _construct to __construct. Now the constructor isn't executed, so the pdo variable will remain empty.
add below lines to catch your exact error in your connection.php
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
this will show your details error exception if any available.
I guess that the variable $getFromU is an instance of the User class. But you have not yet instantiated it before using it.
I'm programming a website on localhost and I found this error when I try to login to submit a new user
Fatal error: Uncaught Error: Call to a member function quote() on null
in C:\xampp\htdocs\recipes\db_manager\users.php:31 Stack trace: #0
C:\xampp\htdocs\recipes\users_manager\checkSubmit.php(15):
notExistsUser('usertest') #1 {main} thrown in
C:\xampp\htdocs\recipes\db_manager\users.php on line 31
checkSubmit.php:
<?php
session_start();
include_once("../db_manager/common.php");
if(isset($_POST["username"]) && isset($_POST["pwd"])) {
$username = $_POST["username"];
$pwd = $_POST["pwd"];
if(notExistsUser($username)){
submit($username, $pwd);
redirect("../index.php", "User successfully submitted!");
} else {
redirect("../submit.php", "This username already exists");
}
}
?>
funcion notExistsUser in users.php
function notExistsUser($username) {
$db = attachdb();
$user = $db->quote($username); //Line 31
$rows = $db->query("SELECT * FROM Users WHERE username = $user");
if($rows->rowCount() > 0)
return false;
else
return true;
}
function attachdb()
function attachdb() {
$add = 'mysql:dbname=Recipes;host=localhost';
try {
$db = new PDO($add, 'root', 'mysql');
return $db;
} catch (PDOException $ex) {
?>
<p>A database error occurred!.</p>
<?php
return NULL;
}
}
What this error means and how can I fix it?
Your database connection must be wrong. Are you sure your password for root is mysql?
attachdb() will return NULL on connection problems.
// at this point $db IS NULL and not the desired PDO Instance
$user = $db->quote($username); //Line 31
To fix this: You have to make sure the db connection is working and then there is no error.
I'm working on a simple login with mysql using pdo. Everything works fine so now I'm focusing on the error handling. But there's a problem. In my dbconfig I voluntarily saved the wrong psw/email/dbname to create an error.
<?php
session_start();
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'Captive');
define('DB_PASSWORD', 'arancione');
define('DB_DATABASE', 'geochimie');
function get_db() {
$dbhost=DB_SERVER;
$dbuser=DB_USERNAME;
$dbpass=DB_PASSWORD;
$dbname=DB_DATABASE;
try {
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbConnection->exec("set names utf8");
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
catch (PDOException $e) {
header("../pages/errore.php?errore=dbconfig");
}
}
?>
However, no error can be caught so when I prepare a statement using the db connection returned by get_db() I get this:
Uncaught Error: Call to a member function prepare() on null
Here is the php script where I prepare the stmt:
<?php
class Admin {
public function userLogin( $email,$password)
{
try{
$db = get_db();
$stmt = $db->prepare('SELECT id FROM admin WHERE email=:email AND psw=:hash_password');
$stmt->bindParam("email", $email,PDO::PARAM_STR) ;
$stmt->bindParam("hash_password", $password,PDO::PARAM_STR) ;
$stmt->execute();
$count=$stmt->rowCount();
$data=$stmt->fetch(PDO::FETCH_OBJ);
$db = null;
if($count)
{
$_SESSION['uid']=$data->id;
return true;
}
else
{
return false;
}
}
catch(PDOException $e) {
header("Location: ../pages/errore.php?errore=db");
}
}
}
?>
As you can see there is another try-catch but It doesn't work at the same way. Many thanks for your attention!
Ok guys, It seems that I needed to add die() or exit() after the header in the catch blocks. Now I can handle the errors properly.
This question already has answers here:
Fatal error: Call to a member function query() on null
(2 answers)
Closed 5 years ago.
I'm using webstera cpanel for host my web application and here is my code and i'm using session for log in
Fatal error: Uncaught Error: Call to a member function query() on null
in /home/buddhika/public_html/login.php:38 Stack trace: #0 {main}
thrown in /home/buddhika/public_html/login.php on line 38
<?php
#ob_start();
session_start();
?>
<html>
<head>
<?php
include_once('php/db-connect.php');
$error = "";
$msg = "";
if (isset($_SESSION["isLoggedIn"])) {
header("Location: index.php");
}
global $sql;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$sql = "SELECT fname, password FROM login";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
if (($_POST["username"] == $row["fname"]) && (($_POST["pass"]) == $row["password"])) {
header("Location: index.php");
$_SESSION["isLoggedIn"] = true;
$_SESSION["username"] = $row["fname"];
$_SESSION["image"] = $row["adimage"];
//alert();
$msg = "login success.";
} else {
$error = "Enter a valid username/password !!!";
}
}
}
}
db connection class
<?php
class Db {
// The database connection
protected static $connection;
/**
* Connect to the database
*
* #return bool false on failure / mysqli MySQLi object instance on success
*/
public function connect() {
// Try and connect to the database
if(!isset(self::$connection)) {
// Load configuration as an array. Use the actual location of your configuration file
$config = parse_ini_file('./config.ini');
self::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
}
// If connection was not successful, handle the error
if(self::$connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return false;
}
return self::$connection;
}
public function query($query) {
// Connect to the database
$connection = $this -> connect();
// Query the database
$result = $connection -> query($query);
return $result;
}
"non-object" means that the variable ($conn) doesn't refer to an object. Did you assign it to your database class (e.g., $conn = new databaseClass();, or whatever the class you're using is named)?
So I have this pdo.php file that contains my Database connection. This is the function:
function db() {
static $dbh;
if(!isset($dbh)) {
$dsn = sprintf('mysql:host=%s;dbname=%s', SHOP_DB_HOST, SHOP_DB_NAME);
try {
$dbh = new PDO($dsn, SHOP_DB_USER, SHOP_DB_PASSWORD);
} catch(PDOException $e) {
header('Status: 500 Internal Server Error');
echo '<b>Error:</b> Database connection failed.';
exit;
}
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec('SET NAMES utf8');
}
return $dbh;
}
And this is another file in which I need to use db():(It's detecting an ajax call and based on that, supposed to update information)
if(isset($_POST['updateTree'])) {
$page_id = $_POST['page_id'];
$parent_id = $_POST['parent_id'];
$customer_id = $_SESSION['customer_id'];
$stojkovQ = db()->prepare("
UPDATE
cms_pages
SET
topicId = :parent_id
WHERE
page_id = :page_id
AND
customer_id = :customer_id
");
$stojkovQ->execute(array(
'customer_id' => $customer_id,
'page_id' => $page_id,
'topicId' => $parent_id
));
echo 'updated';
}
So after after getting an "Fatal Error: Call to undefined function db()" even tho it's included in my header.php file(containing all the includes and config information for my site). If I try to include it again just above my query it yields another error(Cannot redeclare db())
Any lead towards the resolving of this problem will be of great help.