Retrieve the value of a COUNT(*) in a PHP variable
Solved
Dobby85
Posted messages
17
Status
Member
-
Dobby85 Posted messages 17 Status Member -
Dobby85 Posted messages 17 Status Member -
Hello everyone,
So here is my problem, I am trying to get the number of rows in my database using count(*). I've searched on several sites but none provided a solution to my problem...
I know I need to alias the count:
SELECT COUNT(*) AS total FROM forum
But after that, I don't know how to retrieve the value and display it on the website.
If anyone knows how to retrieve the value of a count(*) into a PHP variable, I would appreciate it if you could let me know.
Dobby85 :)
So here is my problem, I am trying to get the number of rows in my database using count(*). I've searched on several sites but none provided a solution to my problem...
I know I need to alias the count:
SELECT COUNT(*) AS total FROM forum
But after that, I don't know how to retrieve the value and display it on the website.
If anyone knows how to retrieve the value of a count(*) into a PHP variable, I would appreciate it if you could let me know.
Dobby85 :)
8 answers
-
Hello,
You can proceed like this:<?php // Generating the SQL query $SELECT_COUNT = "SELECT COUNT(*) AS total FROM forum"; $result = MYSQL_QUERY($SELECT_COUNT) OR DIE(); $count = MYSQL_FETCH_ASSOC($result); // Displaying the result ECHO 'The total is '.$count['total']; ?>
There you go! -
I copied and pasted your code, making sure to use the correct variables, but nothing is displaying; it doesn’t even read the echo... In fact, it enters the DIE at the line:
$result = MYSQL_QUERY($SELECT_COUNT) OR DIE();
So it doesn't read what's next...
Do you have a solution?
Dobby85 :) -
In my opinion, you are missing the code to connect to the database.
To check:$result = MYSQL_QUERY($SELECT_COUNT) OR DIE(mysql_error());
If this is indeed the problem, I recommend following tutorials (for example on the site du zéro) to get started with using mysql with PHP, as you need comprehensive training, not just a simple push. -
I've already read the entire book on PHP and MySQL from Site du Zéro... And I can connect to the database, but I just want to retrieve a numeric value into a variable...
And the:
OR DIE (mysql_error());
doesn't display anything, I know it goes inside but it doesn't show anything... :(
I've seen on other sites that you should alias the count(*):
SELECT COUNT(*) AS total FROM forum
and that the result is in total, but I can't retrieve this result...
Thanks in advance
Dobby85:) -
If the OR DIE (mysql_error()); does not display anything and the following echo doesn't either, then this part of the code is not being executed.
I know it enters here but it doesn't show anything..
How do you know it enters here? I mean: what do you do to make sure? Because displaying a result from a query, COUNT() or not COUNT(), is still basic.
Can you share your code (without passwords of course) so we can understand? -
```php I know it goes inside because instead of using mysql_error() I wrote a word and it was displayed afterwards. That's exactly what annoys me, I suspect it's super simple but I can't find it!! :(
Here is my code :
<?php
try
{
$bdd = new PDO('mysql:host=sql.franceserv.fr;dbname=mabase', 'identifiant', 'mot_de_passe');
}
catch (Exception $e)
{
die('Error: ' . $e->getMessage());
}
// Generating the SQL query
$SELECT_COUNT = $bdd->query("SELECT COUNT(*) AS total FROM forum");
$result = MYSQL_QUERY($SELECT_COUNT) OR DIE(mysql_error());
$count = MYSQL_FETCH_ASSOC($result);
// Displaying the result
echo 'The total is ' . $count['total'] .'';
And this code doesn't display anything, it goes into mysql_error()...
Thank you for your responses!
Dobby85:) ``` -
You must have misread the book from the Site du Zéro on PHP and MySQL.
Your code mixes two ways of accessing a database: PDO and the "classic" functions. If you copied Panoramix’s code without realizing the problem, it means you’ve probably never written a single line of MySQL yourself. This is not a complaint; everyone has to start somewhere...
Here is the PDO version that I recommend you use. Your connection is in PDO, which is convenient.<?php // Generating the SQL query $SELECT_COUNT = "SELECT COUNT(*) AS total FROM forum"; $result = $bdd->query($SELECT_COUNT); $count = $result->fetch(PDO::FETCH_ASSOC); // Displaying the result ECHO 'The total is '.$count['total']; ?>
-
Thank you very much, indeed, that's one thing I didn't understand, the different ways to connect... And I still don't understand, but that's okay, it works perfectly and I thank you greatly because I've been searching for this for a long time!!! :)
Thank you very much!
Dobby85:)