Re: Need help with Database and variables

From: Date: Tue, 08 May 2001 08:46:48 +0000
Subject: Re: Need help with Database and variables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-51873@lists.php.net to get a copy of this message
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:ryan@ramulus.com

« previous php.general (#51873) next »