Incorrect SQL Statement (debug) - php

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?

Related

prepare statement give error

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.

Call to a member function bind_param() on boolean ,mysqli_stmt::store_result and mysqli_stmt::close()

here is my code... it's about using php mysqli extension
<?php
error_reporting(E_ALL);
$db = new mysqli("localhost","root","","dee");
if ($db->connect_errno)
{
die('Unable to connect to database');
}
mysqli_set_charset($db,"utf8");
$storeid=4;
$categoryid=6;
$statement_store = $db->prepare('SELECT * FROM tbl_store WHERE store_id=?');
$statement_store->bind_param('i',$storeid);
$statement_store->execute();
$statement_store->store_result();//---------------(1)
$statement_store->bind_result($store_id,$store_name,$store_description,$store_image,$store_open,$store_close,$store_foldername);
$statement_store->fetch();
$store = $store_name;
//$statement_store->close();//--------------(2)
$statement_category = $db->prepare('SELECT * FROM tbl_category WHERE category_id=?');
$statement_category->bind_param('i',$categoryid);
$statement_category->execute();
$statement_category->bind_result($category_id,$category_name);
$statement_category->fetch();
$category = $category_name;
echo $store;
echo '<br>';
echo $category;
?>
Fatal error: Call to a member function bind_param() on boolean error gives
when not using both (1) and (2)
when use (1) or (2) not gives error
when use both (1) and (2) not gives error
can anybody tell me what was happen here ?
When you don't use store_result() or close() then your first prepared statement (or the result of it) is still "active". This means you must read the data somehow before you can issue a new prepared statement. Because of that your second prepare() statement will fail, it returns the boolean value false.
Check the $db->error field and you will see you get the "Commands out of sync; you can't run this command now" error message. From the MySQL manual B.5.2.14 Commands out of sync:
If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.
This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.

PDO breaks if the query returns an empty result

I'm using this page to convert my queries from mysql to PDO, but it's breaking whenever the database query returns an empty result and gives this error message:
Fatal error: Call to a member function query() on null in ...
This is the line that is producing the error:
$Result =$db_val->query($Query)
Were at that state the $Query is equal to "Select * from cms_roleassign WHERE rid='5' " and that returns nothing.
EDIT:
adding in some bit of the code where $db_val is initialized:
$db_val = new PDO("sqlsrv:server=".$config['DBHostName'].";database=".$config['DBName']
, $config['DBUserName']
, $config['DBPassword']);

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