Jump to content

Strange PHP PDO MySQL Error

DriedSponge
Go to solution Solved by Eigenvektor,

What language (PHP, I assume) and/or library is this? I would assume

$query->execute([$gid]);

to return some form of result object, so you'd have to use that to fetch your results? Similar to this:

$statement = $db->prepare('SELECT * FROM comments WHERE blogpostid = ? ORDER BY created ASC');
$exec = $statement->execute([$_GET['blogpostid']]);
$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);

The error implies that "$query" is a boolean value, which is why you can't call "fetch" on it.

I created a function that checks if a user exist in my data base:

function UserExist($gid)
{
    $query = SQLWrapper()->prepare("SELECT ID FROM Users WHERE gid = ?");
    $query->execute([$gid]);
    $info =  $query->fetch();
    if(empty($info)) {
        return false;
    } else {
        return true;
    }
}

But it gives me this error:

Uncaught Error: Call to a member function fetch() on bool

Any ideas as to why?

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

What language (PHP, I assume) and/or library is this? I would assume

$query->execute([$gid]);

to return some form of result object, so you'd have to use that to fetch your results? Similar to this:

$statement = $db->prepare('SELECT * FROM comments WHERE blogpostid = ? ORDER BY created ASC');
$exec = $statement->execute([$_GET['blogpostid']]);
$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);

The error implies that "$query" is a boolean value, which is why you can't call "fetch" on it.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

What he said. Look at the PHP docs, both the prepare() and the execute() functions regularly return Boolean values. So I would just log your values for both of those executions, and you'll figure out which one is returning a value you weren't expecting.

https://www.php.net/manual/en/pdo.prepare.php


https://www.php.net/manual/en/pdostatement.execute.php

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Eigenvektor said:

What language (PHP, I assume) and/or library is this? I would assume


$query->execute([$gid]);

to return some form of result object, so you'd have to use that to fetch your results? Similar to this:


$statement = $db->prepare('SELECT * FROM comments WHERE blogpostid = ? ORDER BY created ASC');
$exec = $statement->execute([$_GET['blogpostid']]);
$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);

The error implies that "$query" is a boolean value, which is why you can't call "fetch" on it.

I echoed out the value and it's not a boolean, for some reason it's equal to 11.

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

Ok I got it to work by doing  fetchAll(\PDO::FETCH_ASSOC) but I'm still confused on why I have to do that. I have written many SQL SELECT queries before just like the one in my original post and they've worked perfectly fine.

Remember to quote or @mention others, so they are notified of your reply

AMD Ryzen 9 5950X | Arctic Liquid Freezer II 360 | MSI B450 TOMAHAWK MAX | G.Skill Trident Z RGB 32 GB DDR4-3600 | Gigabyte GeForce RTX 2080 SUPER 8 GB 

Samsung 980 EVO Plus 2TB | SK hynix Gold S31 500GB SSD | Seagate Barracuda Compute 2 TB 7200RPM HDD | 1TB Samsung 860 EVO SSD | 3x Phanteks T30-120

Corsair RM1000e 1000 W 80+ Gold Certified Modular PSU | Corsair 5000D Airflow Windows 11 Home

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×