PHP PDO: Call to a member function fetch() on boolean
This line here is one of the reasons. execute returns true or false indicating if the query succeeded or failed.
$query1 = $stmt->execute([$userToGet]);
In a sense, $query1 is a boolean.
Now in these lines, you are trying to access the fetch method from $query1 which is a boolean.
foreach ($query1->fetch() as $row) {
if($row['admin_db'] == 1){
return true;
} else {
return false;
}
}
To get the row, you need to write it like this:
$results = $stmt->fetch();
or in your case:
foreach ( $stmt->fetch() as $row) {
if($row['admin_db'] == 1){
return true;
} else {
return false;
}
}
Error Call to a member function fetch() on boolean in
Try This
<?php
//Connexion PDO
$dsn = 'mysql:dbname=bdd_site;localhost';
$user = 'root';
$password = '';
//afficher les erreurs de connection
try{
$bdd = new PDO($dsn, $user, $password);
}
catch(Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$contenu = $bdd->query('SELECT * FROM commentaire');
//on affiche chaque entrée
while ($donnees = $contenu->fetch()){
?>
<p> commentare de :
<strong><?php echo $donnees['Pseudo']; ?></strong></p>
<p> Note donnée par
<strong> <?php echo $donnees['Pseudo']; ?></strong> : <?php
echo $donnees['Note']; ?></p>
<p> comment :</p>
<p style="color:red;"><?php echo $donnees['Comment']; ?> </p>
<?php
}
$contenu->closeCursor(); //termine traitemennt requête
?>
Fatal error: Call to a member function fetch() on boolean in {path}
Try this instead.
$query = $db_con->prepare(" SELECT SUM(asker_amount) asker, SUM(responder_amount) responder
FROM money WHERE post_id = ? AND author_id = ? AND paid IS NULL");
$query->execute(array($ques_id, $author_ques_id));
$money = $query->fetch(PDO::FETCH_ASSOC);
$asker_amount = $money['asker'];
$responder_amount = $money['responder'];
You should be assigning $money
the result of the final fetch
only.
Fatal error: Uncaught Error: Call to a member function fetch() on boolean in #0 {main} thrown in on line 7
Your PDO constructor is wrong. You are not declaring the database to which the PDO instance should connect to.
$db = new PDO('mysql:host=localhost;root', 'test', '');
The DSN should have the database name and the host name. You have root
at the end of it which should not be there.
The line above should be like so:
$db = new PDO('mysql:host=localhost;dbname=dbnamehere', 'test', '');
Have a look at the documentation in the future and check that your code corresponds with it.
Call to a member function fetch() on integer in
The exec()
method only returns the number of rows effected. You probably want to use query()
instead.
$NU=$connection->query("SELECT COUNT(ID) AS Total FROM USERS");
$Result=$NU->fetch(PDO::FETCH_ASSOC)['Total'];
echo "$Result";
The query()
statement will execute a single query and return a PDOStatement
object you can fetch from or false
on failure.
PHP Fatal error: Call to a member function fetch() on boolean
Please use prepared statement
Remove this part
$sql = "SELECT id FROM music WHERE username = '$name' AND pwd='$pwd'";
$users= $music->query($sql);
$music = $users->fetch();
And Replace it with this
$sql = "SELECT id FROM music WHERE username = :name AND pwd=:pwd";
$statement= $music->prepare($sql);
$statement->execute(array(':name'=> $name,':pwd'=>$pwd));
$music = $statement->fetch();
Related Topics
Generate Seo Friendly Urls (Slugs)
Dynamic Class Method Invocation in PHP
Passing a Variable from One PHP Include File to Another: Global VS. Not
How to Tell If a File Is Text Using PHP
PHP Passing C Struct Data Throught Socket Programming
Why We Need Curlopt_Ssl_Verifypeer in Windows
Looping Through All a Server's Sessions in PHP
Header in PDF Page Using Dompdf in PHP
Prevent Browser's Back Button Login After Logout in Laravel 5
PHP - MySQL Access Denied Error - Works in Other Programs
Trouble Using Posix_Kill in PHP
PHP Failed to Open Stream: Is a Directory
Get Latitude and Longitude Automatically Using PHP, API
Google Drive PHP API - Simple File Upload
PHP and Mod_Fcgid: Ap_Pass_Brigade Failed in Handle_Request_Ipc Function