Jump to content

[SOLVED] Help Grabbing Count


limitphp

Recommended Posts

If I have a query:

$querySongs = "SELECT COUNT(songID)

FROM vote

WHERE songID = '$songID'";

$resultSongs = mysql_query($querySongs) or die (mysql_error());

$rowSongs = mysql_fetch_assoc($resultSongs);

 

How do I grab the COUNT(songID)?

 

like this:

$count = rowSongs[COUNT(songID)];

??

 

thanks....

 

by the way, whoevr wrote that tutorial on UNION JOINS thank you so much....awesome tutorial.  I learned so much about the proper way to structure queries and logically break down what they are doing.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/146167-solved-help-grabbing-count/
Share on other sites

$querySongs = "SELECT COUNT(songID) as SongCount
            FROM vote
            WHERE songID = '$songID'";
$resultSongs = mysql_query($querySongs) or die (mysql_error());
$rowSongs = mysql_fetch_assoc($resultSongs);
echo $rowSongs['SongCount'];

 

Should do it.

 

Using the "AS" keyword allows you to create an alias of the actual field (essentially renames it when it is fetched).

 

EDIT:

To not use the "as" this would also work. And actually I prefer this way for these type of queries:

$querySongs = "SELECT COUNT(songID)
            FROM vote
            WHERE songID = '$songID'";
$resultSongs = mysql_query($querySongs) or die (mysql_error());
$rowSongs = mysql_result($resultSongs, 0);
echo $rowSongs;

 

As you just pull that first column out :)

Archived

This topic is now archived and is closed to further replies.

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