0

So I have this code to count the number of 'Guides' by counting the number of IDs on the table.

    function getGuideCount() {
  global $db;
  $query = $db->query("SELECT COUNT(id) AS count FROM kk_hq_guides ");
  $fetch = $query->fetchAll(PDO::FETCH_ASSOC);
  return $fetch;
}

I am returning the value to a vardump and I am getting this:

array(1) { [0]=> array(1) { ["count"]=> string(2) "36" } } Array

This is correct, I have an array with the key 'Count' storing the amount of guides.

Problem comes when I try to print that value.

$guideCount=getGuideCount();    
print($guideCount['count']);

results in the following error:

Notice: Undefined index: count in ... on line 130. (Line 130 is this:  return $fetch;  )

Big thanks in advance!

2 Answers 2

1

$guideCount[0]['count']

The first item in the $guideCount array is an array containing one element with a key of 'count'.

Sign up to request clarification or add additional context in comments.

2 Comments

Note to OP: The "array-within-an-array" structure you see here is produced by fetchAll(); think of it as "row-within-result" where the result array contains 0 or more rows, and each row array contains 0 or more columns with a value or NULL. If you know that you are only going to have at most one row (as with this simple COUNT() query), you can use fetch() instead to get only the one row. If you use fetch() here, you will be able to access the value by $guideCount['count'].
I did it with just fetch(); instead of fetchAll(); and I got the same result: array(1) { [0]=> array(1) { ["count"]=> string(2) "36" } } EDIT: It did work, I didnt FTP up.
0

First, globals are truly awful. Instead, pass dependencies in where required.

Second, your query will only ever return one row with one column so fetchAll is superfluous.

Try this instead...

function getGuideCount(PDO $db) {
    $query = $db->query("SELECT COUNT(id) FROM kk_hq_guides ");
    return $query->fetchColumn();
}

then...

$guideCount = getGuideCount($db);
echo $guideCount;

5 Comments

I will stick to my global $db; method since I include my database file in the php file where I store my sql query functions. I am not doing OOP on this one so that's why. I do like your approach with fetchColumn(); since this returns a string with the value which is perfect. No need to fiddle with arrays. Thanks!
Dependency injection has nothing to do with OOP. Relying on global state is only going to cause issues later on.
I have to include the database on the file that will send the $db paramenter to the query function, how could this be better than having the database included directly to the file with my queries?
@AlexKvazos Where you include the database file is irrelevant as long as the $db variable is in scope where it is required. Your functions don't need to know anything about the database other than the fact that they will be given a PDO object to work with.
Sounds good. Thanks for this tip. I will use it in my future projects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.