prepare statement give error - php

is there something wrong with my code, it look exactly like the example on the php page but it give me this error Fatal error: Call to a member function bindParam() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/videosharing/index.php on line 68
$hi = 'hi';
$limit = 4;
$isi = 1;
$query = "SELECT `videoname`,`username`,`videourl`,`uploaddate`,`duration`,`views`,`tags` FROM `videolist` WHERE `tags` = :atagz ";
$stmt = $connection->prepare($query);
$stmt->bindParam(':atagz',$hi);

Your connection is likely fine (otherwise, you'd have a different error, sooner).
If the error is "Fatal error: Call to a member function bindParam() on a non-object", then $stmt isn't an object. In other words, your prepare() call is failing. Per the documentation for prepare(), that occurs when the database can't prepare the statement.
Reporting these errors is one of the areas where I think PDO falls short. You can get more information on the error with the following:
var_dump($connection->errorInfo());
The most likely cause is a misspelling in an attribute or table name.

Related

Incorrect SQL Statement (debug)

My controller.php is including the connection to the database, and I'm accessing information from my database with the following code:
function getWidgets(){
$widgetQ = "SELECT * FROM tblfinal WHERE Type = 'Widget'";
global $myPdo;
$myCommand = $myPdo->prepare($widgetQ);
$myCommand->execute();
return $myCommand;
}
However, it doesn't seem to like my SQL statement, and gives me the following error:
Fatal error: Call to a member function prepare() on null on line 11
I think this means that my SQL statement isn't actually pulling anything from my database.
Any tips?

Unexpected PDO "General error 2050" depending on configuration

I have some code where variable $query is supposed to hold different PDOStatement objects, one replaced by another:
$query = $conn->prepare("select * from ... ");
$query->execute();
while ($tmp=$query->fetch(PDO::FETCH_ASSOC)) {
...;
}
//unset($query);
$query = $conn->query("select * from ... ");
while ($tmp=$query->fetch(PDO::FETCH_ASSOC)) {
....;
}
With such code I get "SQLSTATE[HY000]: General error: 2050" from PDO on the line with the last fetch. But if I uncomment line with "unset" - it starts working correctly.
Any idea of what could it be?
PS Without using unset, it also works with PDO::ATTR_EMULATE_PREPARES = true
UPD
Here is error code description from MySQL site:
Error: 2050 (CR_FETCH_CANCELED)
Message: Row retrieval was canceled by mysql_stmt_close() call
in some php versions error 2050 happen because you must empty the var that holds the object with the result of the query
see https://stackoverflow.com/a/36631355/2613863
in Matt Cavanagh commet

call to a member function non-object

I'm beginning with PHP and i need your help.
I create a try to list all members who has the same interest that the current_member( i mean the connected member ).
I write this :
$current_members = params('current_member');
$members_all = option('db')->query('SELECT * FROM members WHERE interest = $current_members["interest"] ORDER BY lastname, firstname')->fetchAll();
set('members_all', $members_all);
When I go on my page I have the error :
Fatal error: Call to a member function fetchAll() on a non-object
And in my view I just write this :
<h2 id="member-<?= $member['id'] ?>">
<?= avatar_tag($member,'30x30') ?>
<?=$member['firstname']?><small> <?= $member['lastname'] ?></small>
</h2>
I dont understand this error, anyone can help me ?
Thank's for your help.
Do not chain calls to query() and fetchAll() like you are doing. That's bad practice. Never assume your query worked, always check to see if it did.
$db = option('db');
$query = $db->query('SELECT ...');
if($query === FALSE){
print_r($db->errorInfo());
die;
}
$members_all = $query->fetchAll();
(Since you are calling fetchAll(), I assume you are using PDO (and not MySQLi))
Also, do not try to concatenate variables into your SQL query. (P.S. You're not even doing that, you are using single quotes, so $current_members["interest"] is not being read as a variable) That's just asking for an SQL injection attack. What you want to do is use prepared statements.
$db = option('db');
$query = $db->prepare('SELECT * FROM members WHERE interest = ? ORDER BY lastname, firstname');
$exec = $query->execute(array($current_members['interest']));
if($exec === FALSE){
print_r($query->errorInfo());
die;
}
$members_all = $query->fetchAll();
The "call to a member function on a non-object" error, means that you are trying to call a method on a variable that does not represent an object.
You have the following methods called one after the other on the $members_all denifition:
option('db')->query("...")->fetchAll();
You call the method "query" of whatever returns option('db') with some SQL query, and then you call fetchAll() method to whatever returns that "query" method.
I do not know if I explained myself well, the main point is that when you execute the query method it is returning something that has not the "fetchAll" method, in your case your SQL is wrong and probably query() is returning NULL or FALSE instead of a result set.
Change your single quotes with double quotes or concatenate the $current_member['interest'] variable.
IN your sql query
'SELECT * FROM members WHERE interest = $current_members["interest"] ORDER BY lastname, firstname'
you are using single quotes, so $current_members["interest"] actually does not resolve to a PHP variable, it is a string. You can switch single and double quotes:
Made an edit here, passing array offset was not fortunate:
$interest = $current_members['interest'];
"SELECT * FROM members WHERE interest = $interest ORDER BY lastname, firstname"
Unfortunately you did not share any of the underlying database code, but assuming option('db') is a pdo object, this should work fine.
If option('db') really is a pdo, before executing any statement add:
option('db') -> setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
this will tell you the exact error

Why am I getting a "PHP Fatal error: Call to a member function bind_param() on a non-object" while using transactions and prepared statements?

Like the title says, I am attempting to use both PHP/MySQLi transactions and prepared statements, but I keep getting the "PHP Fatal error: Call to a member function bind_param() on a non-object" error, and I cannot figure out why.
<?php
$dbConnection = new mysqli($hostname,$username,$password,$database);
$query01 = "delete from model where make_id = ?";
$query02 = "delete from make where id = ?";
if($stmt01 = $dbConnection->prepare($query01) && $stmt02 = $dbConnection->prepare($query02)){
if($stmt01->bind_param('i',$id) && $stmt02->bind_param('i',$id)){
if($stmt01->execute()){
if($stmt02->execute()){
$dbConnection->commit();
$dbConnection->commit();
}
else{
$dbConnection->rollback();
$dbConnection->rollback();
}
}
else{
$dbConnection->rollback();
}
}
else{
// do something ...
}
}
else{
// do something ...
}
?>
What I was doing when I got the error, was testing the rollback should an error occur. I replaced the prepare "if" statement with "false", and everything worked great. I replaced the bind "if" statement with "false", and everything worked out great. However, when I replaced the $stmt01->execute() "if" statement with a "false", this is where I got the error. The weird thing is that I got the error on the bind "if" statement line (the line above where I just tested with "false") and not the line itself.
Thank you for your help.

MySQLi Fatal error

I have index.php page and in the title tag I have something like this:
<title><?php echo getBasic('title'); ?></title>
And it's returning the following error:
Fatal error: Call to a member function bind_param() on a non-object in C:\Program Files\WAMP\www\Filmovi\modules\database\dbcon.php on line 12
And in dbcon.php included on the top of the index with require_once('modules/database/dbcon.php') I have this:
function getBasic($type){
global $db;
$sql='SELECT content FROM a853_filmovi WHERE type = ?';
$stmt = $db->prepare($sql);
$stmt->bind_param('s',$type); <-- Line 12
$stmt->execute();
$stmt->bind_result($content);
return $content;
}
On the line number 3 I have this:
$public = getBasic('public');
and it's working perfectly.
By the way, this worked and showed the title properly and then stopped working because of an uknown reason. I don't get it how is it working with getBasic('public') but not with the title. I have a record with the type 'title' in the database so that's not a problem.
Thanks in advance.
Errors like this happen because you are not checking return values before using them.
In this case the error happens because $db->prepare($sql) fails, returns false, and then you use it as if it is a statement (stmt) object.
Check your return values before using them:
$stmt = $db->prepare($sql);
if ($stmt === false) {
die('Preparing SQL string failed');
}
One reason for the error is, prepare() is getting failed -
if the sql statement sent to it is not valid in the current DB.
prepare() will then return false.
Eg - if the table name is not correct or one or more field in the query does not exist.

Categories