Jump to content

How to do a "SELECT count(*)"


simpson_121919

Recommended Posts

I am trying to use a SELECT count(*) to check if a given record exists but its not working and always returns FALSE. I am stumped. At the very least I would think it would return 0. I have checked the SELECT statement in myphpAdmn to make sure that was not the issue. Here is my code -

$Qchoice = "SELECT count(*) FROM collect_temp_table WHERE user_ID=:userID AND gID=:gID AND rID=:rid";
            $QchoicePreped = $dbh->prepare($Qchoice);
            $QchoicePreped->bindParam(':userID',$userID,PDO::PARAM_STR,75);
            $QchoicePreped->bindParam(':gID',$gID,PDO::PARAM_STR,;
            $QchoicePreped->bindParam(':rID',$rID,PDO::PARAM_STR,3);
            $QchoicePreped->execute();
            $QchoiceResult = $QchoicePreped->fetch(PDO::FETCH_NUM);

Any help would be appreciated, thanks.

Link to comment
https://forums.phpfreaks.com/topic/285110-how-to-do-a-select-count/
Share on other sites

$QchoiceResult is an array. if you are not referencing $QchoiceResult[0], you won't get the selected count(*) value from the query.

 

edit: you also have a spelling/capitalization error between the :rid placeholder in the query and the bind statement that is either throwing a php warning or an exception, depending on what pdo error mode you are using. you always need to test for errors before you can use the result from any step in your code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.