Jump to content

How to do a "SELECT count(*)"


simpson_121919
Go to solution Solved by mac_gyver,

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
Share on other sites

  • Solution

$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.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.