You need to fetch/get information out of the database using a function,
since we want just one result (count) we can easily use mysql_result()  
Other functions that you're probably familiar with are mysql_fetch_row and
mysql_fetch_array ... they'll work too but in this case, not as 
efficiently.  In its simplest form, consider :

    $r     = mysql_query('SELECT count(*) FROM tablename');

    $count = mysql_result($r,0);

That's about as simple as you'll get.  Add a little check (change the
second line to) :

    // if $r is true, return the result, otherwise return 0
    $count = ($r == true) ? mysql_result($r,0) : 0;

And for informational purposes (regarding your question), a while loop is
not required, the following will get one row of data :

    $row = mysql_fetch_array($result);

    print $row['name'];

If we wanted to return/print/get multiple rows of data, we'd use a loop :

   while ($row = mysql_fetch_array($result)) {

       print $row['name'] ."<br>\n";
   }

Essentially it means, "while there are rows of data left, keep on
assigning a row of data to $row so i can use it for stuff"  OR "while 
$row = mysql_... is true, keep going."  But I digress.

Back to the topic on hand, there's a faq on this, it can be seen here :

    http://www.faqts.com/knowledge_base/view.phtml/aid/114/fid/12

And to further digress, the above  ? :  use is sometimes called a 'ternary
operator' and can be read about here (see google if interested) :

    http://www.php.net/manual/en/language.expressions.php

It's essentially like an if/else statement and totally unrelated to this
thread but shorter to write, well, was until I wrote this part :)

Regards,
Philip

> > I was wondering if I do a database query like "SELECT count(*) FROM
> > table_name AS song_count" if there is a way I can assign song_count to a
> > variable that is accessable anywhere on the page and not just in a "WHILE"
> > statement?
> >
> > Thanks is advance
> >
> > Ryan
> > mailto:[EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to