0

Basically I am trying to return the total number of columns that correspond to a given criteria:

$exec = $link->query("SELECT COUNT(*) FROM `requests` WHERE language='PHP'"); $result = $exec->fetch(PDO::FETCH_ASSOC); echo $result[0]; 

However, the above does not return anything but the SQL query is correct since it returns a value when executed in phpMyAdmin.

1 Answer 1

3

Since you explicitly used the flag PDO::FETCH_ASSOC, you need to point on the associative index it returns. I'd suggest put an alias on the count()

SELECT COUNT(*) AS total FROM `requests` WHERE language='PHP' 

Then access it after fetching:

echo $result['total']; 

Another way would be to use ->fetchColumn():

$count = $exec->fetchColumn(); echo $count; 
Sign up to request clarification or add additional context in comments.

3 Comments

I actually never thought of looking into the query itself. Thank you for your time. Will accept it as soon as possible.
@schmitsz pointing into index [0] should have given you an undefined index actually, so you should turn on error reporting always. im glad this helped
A bad habit of mine is often doing error_reporting(0); right after the opening tag.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.