I get this error:
Fatal error: Call to a member function prepare() on a non-object in /home/folder/public_html/includes/name.php on line 1768
This is the function:
function _FC_GET($typ, $data, $username){
global $dbh;
$stmt = $dbh->prepare("SELECT * FROM stats_clicks WHERE typ=:typ AND user=:username AND data=:data LIMIT 1;");
$stmt->bindParam(':typ', $typ);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':data', $data);
try {
$stmt->execute();
}
catch(PDOException $e) {
die( _OP_ERROR($e->getMessage(), __FILE__ ,__LINE__) );
}
$QUERY_DAT = $stmt->fetchAll();
return empty($QUERY_DAT['value']) ? 0 : $QUERY_DAT['value'];
}
And this is line 1768:
$stmt = $dbh->prepare("SELECT * FROM stats_clicks WHERE typ=:typ AND user=:username AND data=:data LIMIT 1;");
I can't seem to find what is causing this. I use the $dbh-prepare(); statement in other functions in the same file.
$dbh is not defined at that line. Check where you create the object $dbh if it is before the line 1768 or it is not in any condition which is not fulfilled.
Try this :
Look at here
Probably the cause of your connection.
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 getting the error:
Call to a member function fetch() on a non-object
The line this refers to is:
$getProjectIdResult = $stmt->fetch();
Now, I think from this error that there must be something wrong with my database query, since the documentation says PDO query returns false on failure. I'm having trouble identifying what is causing the issue.
I've tried wrapping the fetch in a try/catch, with
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
However the catch isn't triggered and I just get the original fatal error so I haven't been able to get a more specific error.
classes.php
class Query extends Connection {
public function getProjectID($surveyID) {
$query_getProjectID = "SELECT projectID FROM test WHERE surveyID = :surveyID";
$query_getProjectID_params = array(
':surveyID' => $surveyID
);
try {
$stmt = $this->db->prepare($query_getProjectID);
$stmt = $stmt->execute($query_getProjectID_params);
}
catch (PDOException $ex) {
die("Failed to get project ID: " . $ex->getMessage());
}
$getProjectIdResult = $stmt->fetch();
$getProjectID = $getProjectIdResult['projectID'];
return $getProjectID;
}
}
test.php
include_once("includes/classes.php");
include_once("includes/functions.php");
// Bind $_GET data
// localhost/panel/test.php?surveyID=3&status=1&respondentID=666
// Expected result: 111
$surveyID = sanitise($_GET['surveyID']);
$status = sanitise($_GET['status']);
$respondentID = sanitise($_GET['respondentID']);
$con = new Connection();
$query = new Query();
$query->getProjectID($surveyID);
$con->closeConnection();
I've ruled out the sanitise function causing an issue by testing with and without it.
I apologise as I know this is probably just another amateur making another amateur mistake judging by how many posts there are by the same title.
When you call
$stmt = $stmt->execute($query_getProjectID_params);
You assign the return-value of execute() to $stmt, overwriting the variable, making it a boolean instead of an object. When you continue, $stmt no longer holds the PDOStatement object, but is now a boolean.
The solution is simply to remove the overwrite of your object, like this (remove $stmt = in front).
$stmt->execute($query_getProjectID_params);
http://php.net/pdostatement.execute
I red several questioins, but no one helped.
Fatal error: Call to a member function bind_param() on boolean in -> nope.
Fatal error: Call to a member function prepare() on null -> nope.
Fatal error: Call to a member function count() on boolean -> nope.
Fatal error Call to a member function prepare() on null -> nope.
fatal error call to a member function prepare() on resource -> nope.
Error: Call to a member function prepare() on a non-object -> nope. I am done..
I am using PHP5 and mySql with PDO:
Connection and Select works fine, but the Insert didnt want to work.
That's my function:
function AddNewUser($nickname, $email)
{
ini_set('display_errors', 1); //DELETE ME
ini_set('expose_php', 1); //DELETE ME
$pdo = EstablishDBCon();
echo "Subscribe user..<br/>";
$sql = "INSERT INTO db.table (nickname, email, insertdate, updatedate) VALUES (:nickname, :email, :insertdate, :updatedate)";
try {
$stmt = $pdo->prepare($sql); //Error at this line
//id?
$stmt->bindParam(':nickname', $nickname, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':insertdate', date("Y-m-d H:i:s"), PDO::PARAM_STR);
$stmt->bindParam(':updatedate', null, PDO::PARAM_NULL);
$stmt->exeute();
CloseDBCon($pdo);
echo "Subscribed!<br/>";
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
The DB pattern is:
id (int not null auto_inc) | nickname (varchar not null) | email (varchar not null) | insertdate (datetime) | updatedate (datetime)
I am new to php and I do not understand that type of error.
I marked the line inside the code, where the error is thrown:
$stmt = $pdo->prepare($sql); //Error at this line
Can someone help me?
Thanks in advance!
//EDIT:
Connection aka db_connection.php:
<?php
echo 'Establishing MySQL Connection<br/>';
$pdo = null;
$dsn = 'mysql: host=xx; dbname=xx';
$dbUser = 'xx';
$pw = 'xx';
try {
$pdo = new PDO($dsn, $dbUser, $pw);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connection established.<br/>';
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
return $pdo;
?>
Here is the EstablishDBCon function:
function EstablishDBCon()
{
$pdo = include_once 'db_connection.php';
return $pdo;
}
The best way to reuse functions is to put it inside of the include file, then include it at the top of each file you'll need it. So inside of your db_connection.php, create your function:
function EstablishDBCon()
{
$pdo = false;
try{
// Put your PDO creation here
} catch (Exception $e) {
// Logging here is a good idea
}
return $pdo;
}
Now you can use that function wherever you need it. Make sure you always make sure $pdo !== false before you use it, to make sure your connection hasn't failed.
The problem is in the function EstablishDBCon(), which expects the include_once statement to return a value as if the contents of the included file are a function.
function EstablishDBCon()
{
$pdo = include_once 'db_connection.php';
return $pdo;
}
But that's not how include_once works here:
if the code from a file has already been included, it will not be included again, and include_once returns TRUE.
That's why you end up with TRUE (a boolean) in your $pdo variable.
In any event, this kind of construction makes your code really hard to follow.
I recommend only using include and friends to combine self-contained PHP functions together, or to embed parts of HTML pages in one another.
Call to a member function on boolean in this case means that $pdo is not an object, it's a boolean. So it's likely that EstablishDBCon() is returning either a true on success or false otherwise, as opposed to a database resource. Double-check the docs on that function. Here's a link to some relevant documentation on PDO that you'll need.
I don't why I'm getting a call to a member function fetch() on a non-object in pdo error. It's really frustrating me!
$db= new PDO($dns,DB_USER,DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$workspace_id=isset($_GET['workspace']) ? (int)$_GET['workspace'] : "";
try{
$sql="SELECT * FROM messages WHERE workspace_id = :id";
$stmt=$db->prepare($sql);
$stmt->bindValue(':id', $workspace_id, PDO::PARAM_INT);
$result=$stmt->execute();
$message=$result->fetch();
}catch(Exception $e){echo $e->getMessage();}
execute()
Returns TRUE on success or FALSE on failure.
In your case you are try to fetch data form bool value
$result=$stmt->execute();
$message=$result->fetch();
Just need to change
$stmt->execute();
$message=$stmt->fetch();
Because $stmt->execute() should return boolean value.
If you want to use the fetch you should call method from $stmt
Documentation: PDOStatement
Hi I seem to be getting an error on my code that seems to say that I have not initialez my variable but th veriabile is initialized in the constructor:
Notice: Undefined variable: conn in D:\Program Files\xampp\htdocs\Tutorials\Login\classes\mysql.php on line 18
Fatal error: Cannot access empty property in D:\Program Files\xampp\htdocs\Tutorials\Login\classes\mysql.php on line 18
And this is my code:
<?php
require_once('includes/constants.php');
class Mysql{
private $conn;
function __construct() {
$this->conn = new mysqli(DB_SERVER , DB_USER , DB_PASSWORD , DB_NAME) or
die('There was a problem connecting to the database');
}
function verify_Username_and_Pass($un , $pwd){
$query = "SELECT *
FROM users
WHERE username = ? AND password= ?
LIMIT 1";
if($stmt = $this->$conn->prepare($query)){ //this is where the error points
$stmt->bind_param('ss', $un, $pwd);
$stmt->execute();
if($stmt->fetch()){
$stmt->close;
return true;
}
}
}
}
?>
The error is thrown at the first if statement.How can I solve it?
Replace
$this->$conn
with
$this->conn
on the line when you have the error.
$this->$conn
This is wrong, it should be
$this->conn