Jump to content

PHP PDO Count - How do you retrieve the result


wongadob
 Share

Recommended Posts

Hi all, Sorry for this almost certain newbie question, but I have searched here for the answer and SQL references say how to use count, but do not say about using PDO how you actually get the result. PDO reference manuals don't focus too much on the specific of SQL so therefore they don't discuss using count. It has kind of fallen between the gaps. So here is my problem:- This is my code. Assume I have a connection ($myConnection) as I know this works, it is used a lot before this.

  $sqlString = "SELECT COUNT(*) FROM users WHERE useraccess=?";  $sth = $myConnection->prepare($sqlString,array(PDO::ATTR_CURSOR=>PDO::CURSOR_FWDONLY));  $sth->execute(array(ACCESS_ADMIN));   $row = $sth->fetch(PDO::FETCH_NUM);  Print "I got here".$row;

ACCESS_ADMIN is a define equating to a value. As you can see I want to count the number of users with a certain access code level. I have done many other queries for returning actual data which arrives in the array $row, but I am not sure, when I am doing a count, how it gets returned and how I retrieve the answer. The print statement prints "I got here" and that is all. $row contains nothing even if I put $row[0] nothing gets printed. The answer should be 3. Any help as always, much appreciated.

Link to comment
Share on other sites

Did you tried var_dump() the $row? Probably it is returning false.

Link to comment
Share on other sites

I tried a print_r ($row) and that had nothing in it. I know that answer should be 3. Just not sure what I should be testing on return. I feel that it may not be returning an array of data as it does for select, but I don't know what else to check or if there is some parameter I am supposed to set in the Prepare.

Link to comment
Share on other sites

Thanks, It is false. So there is an error, but do you know why? I prepare the statement in exactly the same way for every other type of query (i.e. not count) the parameters, variables etc are all corrrect and the expected count result is 3. Are there some other setup parameters when doing a count or is there was some other way the data is returned, other than through an array of values? Any ideas?

Link to comment
Share on other sites

Thanks guys, My stupid mistake. Incorrect variable name. When defining $myConnection as a global. I had not capitalised the C. Normaly I copy and paste this line, but this time I must have typed it instead, hence the mistake. Doh! I know you should never say 'I know this bit works...' as soon as you utter those words it alway is that bit! At least now I know that it does return the count in $row[0]. Thanks for your helpful advice (as always) Best Regards

Link to comment
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
 Share

×
×
  • Create New...