I booted up wamp today to check something for one of my website and ran across a php error
[30-Jan-2022 16:57:28 UTC] PHP Fatal error: Uncaught Error: Call to a member function prepare() on boolean in C:\wamp64\www\includes\class.database.php:130
Stack trace:
When checking the php file in question i have no idea how to fix this, anyone that can help?
// Allow for prepared arguments. Example:
// query("SELECT * FROM table WHERE id = :id", array('id' => $some_val));
$sth = $this->db->prepare($sql);
$debugSql = $sql;
$params = array();
if (is_array($args_to_prepare))
{
foreach ($args_to_prepare AS $name => $val)
{
$params[':' . $name] = $val;
$debugSql = preg_replace('/:'.$name.'/',"'".$val."'", $debugSql);
}
}
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:
Click this
Related
I already tried other solutions from similar questions but it seems that nothing worked. Like the title says, the query I need to execute on my db works fine on phpMyAdmin, but it doesn't work when used inside a php function. This is the code in common_functions.php:
function eliminaRistorante($id_ristorante) {
global $conn;
// query su ristorante
$sql = "DELETE FROM ristorante WHERE id_ristorante = $id_ristorante";
echo $sql;
if($conn->query($sql) === TRUE) {
return true;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
return false;
}
}
This is the code in hold.php:
include('includes/logic/common_functions.php');
if (isset($_POST['del'])) {
$id_ristorante = $_POST['del'];
eliminaRistorante($id_ristorante);
header('location: admin/ristorante.php');
}
I put an echo statement to see if the query was effectively correct.
This is the output:
DELETE FROM ristorante WHERE id_ristorante = 5
Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\projects\db_ristorante\includes\logic\common_functions.php:57 Stack trace: #0 C:\xampp\htdocs\projects\db_ristorante\hold.php(8): eliminaRistorante('5') #1 {main} thrown in C:\xampp\htdocs\projects\db_ristorante\includes\logic\common_functions.php on line 57
Lastly, I tried these solutions:
MySql query not in working in PHP but works in phpMyAdmin
Inside that question the user kindly shared other solutions that I considered too.
Uncaught Error: Call to a member function query() on null in
your $conn Object is null. You did not initialise a PDO instance.
Here is a introduction, on how to do that, PHP The Right Way - PDO
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I am working on a project where I have a DB_Functions.php file to store all needed PHP functions.
Here is one of them:
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT id, email, encrypted_password, salt,imagen,nombre,apellidos,created_at,unique_id,nivel_usuario,verified,cel_verificado,tel FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$stmt-> bind_result($token1,$token3,$token4,$token5,$token6,$token7,$token8,$token9, $token10,$token11,$token12,$token13,$token14);
while ( $stmt-> fetch() ) {
$user["id"] = $token1;
$user["email"] = $token3;
$user["encrypted_password"] = $token4;
$user["salt"] = $token5;
$user["imagen"] = $token6;
$user["nombre"] = $token7;
$user["apellidos"] = $token8;
$user["created_at"] = $token9;
$user["unique_id"] = $token10;
$user["nivel_usuario"] = $token11;
$user["verified"] = $token12;
$user["cel_verificado"] = $token13;
$user["tel"] = $token14;
}
// verifying user password
$encrypted_password = $user['encrypted_password'];
// check for password equality
if (password_verify($password, $encrypted_password)) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
I am using the same file DB_Functions.php on two different servers.
Server 1 is a hosted web server with PHP 7.4 (ea-php74).
Server 2 is a AWS Ubuntu instance with PHP 7.4.11 (Zend).
On server 1 I am not getting any error using above mentioned function.
On server 2 I am getting following error:
PHP Fatal error: Uncaught Error: Call to a member function bind_param() on bool in /var/www/html/android_api/include/DB_Functions.php:1942\nStack trace:\n#0 /var/www/html/android_api/login.php(17): DB_Functions->getUserByEmailAndPassword()\n#1 {main}\n thrown in /var/www/html/android_api/include/DB_Functions.php on line 1942
What should I do to get the function working on server 2?
Have a look to what does method mysqli::prepare return https://www.php.net/manual/fr/mysqli.prepare.php
"mysqli_prepare() returns a statement object or FALSE if an error occurred."
Error says that $smtp is a bool and you are trying to use bind_param method on a bool. Have a look to the last $smtp assignation. Regarding what does the prepare method can return, it look like the method returned false. So there were an error while trying to prepare the statement.
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 have the code in question:
function get_installed_languages()
{
global $sql;
$languages = Array();
$sql->query("SELECT short_name FROM languages ORDER BY short_name");
if($sql->getNumRows()==1)
{
$languages[0] = "default";
return $languages;
}
else
{
for ($i=0; $i<$sql->getNumRows(); $i++)
{
$get = $sql->getRow($i);
if(trim($get['short_name'])=='')
$languages[$i]='default';
else
$languages[$i] = strtolower($get['short_name']);
}
return $languages;
}
}
It always returns:
Fatal error: Call to a member function query() on a non-object
The problematic line is:
(742): $sql->query("SELECT short_name FROM languages ORDER BY short_name");
Here is the complete file.
Thanks for your help. It turned out to be an invalid path in the DB.
I am really graceful for your efforts in trying to help me!
Otherwise, you can check the whole file (if still interested). A link to it has been posted at the end of my original comment.
Cheers!
I have this function:
public static function getOrdini($sort_order = 4)
{
$con = Propel::getConnection();
$sql = "select * from shop_orders LEFT JOIN shop_orders_total
ON
shop_orders.orders_id = shop_orders_total.orders_id
AND
shop_orders_total.sort_order = :sort_order";
$stmt = $con->prepare($sql);
$result = $stmt->execute(array(':sort_order' => $sort_order));
$ordini = self::populateObjects($stmt);
return $ordini;
}
When I call it I get this error:
( ! ) Catchable fatal error: Object of
class Criteria could not be converted
to string in
/home/javier/Aptana_Studio_Workspace/dev_repo/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel/util/DebugPDOStatement.php
on line 99
but if write the function in this way below I don't get any error:
public static function getOrdini()
{
$sort_order = 4;
$con = Propel::getConnection();
...
Any idea?
Regards
Javi
No bug in the above code it is okay. I tried passing value in the static method and it is fine the error is being generated from the other part of your code checking in the class Criteria would help you here is nothing in the posted.
Ask to propel, symfony. here someone is facing the same problem
http://symfonyexperts.com/question/show/id/51