MySQLi Fatal error - php

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.

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?

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.

Cant update this query

Hi can anyone help me on this
$stmt = $conn->prepare("update data set anrede=?, vorname=?, nachname=?, strasse=?, plz=?, ort=?, krankenkasse=?, seit=?, personen=?, telefon=?, termin=?, time=?, vermittler=?, coment=?,feedback=?, Astatus=?, positiv=?, personen_amgaben=?, fr_1=?, fr_2=?, z_fr_2=?, fr_3=?, z_fr_3=?, fr_4=?, z_fr_4=?, fr_5=?, fr_6=? where t_id=?");
$stmt->bind_param('sssssssssssssssssssssssssssi',$anrede, $vorname, $nachname, $strasse, $plz, $ort, $krankenkasse, $seit, $personen, $telefon, $termin, $time, $vermittler, $coment, $feedback, $Astatus, $positiv, $personen_amgaben, $fr_1, $fr_2, $z_fr_2, $fr_3, $z_fr_3, $fr_4, $z_fr_4, $fr_5, $fr_6, $t_id);
I got this error --- Fatal error: Call to a member function bind_param() on a non-object in
what's wrong
The error indicates that $stmt is not an object. This can happen when $conn->prepare() fails. As from the documentation:
mysqli_prepare() returns a statement object or FALSE if an error occurred.
So, you should check whether the return value is false, and then check what the error is, which you can do with the mysqli error method:
Returns the last error message for the most recent MySQLi function call that can succeed or fail.
So your code could look like this:
stmt = $conn->prepare(" ... ");
if (stmt === false) {
die($conn->error);
}
$stmt->bind_param( ... );
Note that in production mode you better not print database error messages to the browser, but in a log file.
From comments it appears that the above code prints:
Unknown column 'positiv' in 'field list'
which is a clear indication of why the statement failed. Your table data apparently has no column that is called positiv. So correct that typo (maybe it is called positive?), and then try again, fixing any other errors that might be reported in this way.

Call MySql Stored Procedure from PHP (PDO)

I am trying to execute a MySQL Stored Procedure using PDO connection, i tried almost everything but not able to execute it.
The SP will only insert and update. Following is the codes I tried till now.
$config = require('protected/config/main.php');
try
{
$db_adat = new PDO($config['components']['db']['connectionString'], $config['components'] ['db']['username'], $config['components']['db']['password']);
$result= $db_adat->prepare('CALL pdv()');
$a = $result->execute();
$a->fetchAll(PDO::FETCH_ASSOC);
I tried with only fetch(), with only fetchAll(), fetchObject(), with fetch(PDO::FETCH_ASSOC), with fetchAll(\PDO::FETCH_ASSOC), but I always get following error
Fatal error: Call to a member function fetchAll() on a non-object in D:\ADAT_System\www\test\protected\controllers\ExportPDVController.php on line 35
I also tried using query() instead of execute(), but that doesn't work either.
I also tried adding a (select * ) statement in SP and tried with all above "fetch" options, but got same error.
The SP takes 7 minutes to complete, but all gave error immediately, so I am guessing it never ran the SP.
I tried as following too
$result= $this->$db_adat->prepare("CALL pdv()");
$result->execute();
but the I got following error:
Object of class PDO could not be converted to string
I am not passing any parameters in SP, just a simple call. Please let me know if any more information is required.
This part of your code is wrong
$result= $db_adat->prepare('CALL pdv()');
$a = $result->execute();
$a->fetchAll(PDO::FETCH_ASSOC);
Because execute() returns a boolean upon success or failure.
You cannot use that to fetch.here is the proper way with appropriate variable names:
$stmt= $db_adat->prepare('CALL pdv()');
$success = $stmt->execute();
if($success){
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}else{
echo 'failed to run sp';
}
<?php
if(isset($_POST) && !empty($_POST)){
$SearchPO = (($_POST)['SearchPO']);
}
$stmt = $pdo->prepare("CALL spPO(?)");
$stmt->bindParam(1, $SearchPO, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT|PDO::ATTR_EMULATE_PREPARES, 4000);
$stmt->execute();
do {
$result = $stmt->fetchAll();
} while ($stmt->nextRowset() && $stmt->columnCount());
?>
You must use the nextRowset() function because the next record is 'empty' without it. Source

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.

Categories