coolego1 Posted June 30, 2007 Share Posted June 30, 2007 Hello, I am trying to make database calls on what will potentially be a very large database. I would like to receive query results from the database using mysql_fetch_array() to get an associative array. Once I have this array, I want to show certain fields using my own formatting. If there are no fields, I want to show a "No Results" message. My code thusfar looks like this, and it does nothing if there is an invalid database query: $result = mysql_query($sql, $connection); if ($result) { while ($list = mysql_fetch_array($result, MYSQL_ASSOC)) { print $list['title']; print "<br>"; print $list['pages']; print "<br><br>"; } } else { echo "No Results Found"; } This will show the information but will not respond if there is nothing to show... Help? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 30, 2007 Share Posted June 30, 2007 try instead of saying if($result) (That only test if query actually executes, not num results) try if(mysql_num_rows($result) >0) Quote Link to comment Share on other sites More sharing options...
Tandem Posted June 30, 2007 Share Posted June 30, 2007 mysql_query() only returns false if there is an error, so it'll still return true even if there are no results returned. As cooldude832 said, you'll need to count the results first. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 $result = mysql_query($sql, $connection); $value=mysql_fetch_assoc($result, MYSQL_ASSOC); if(!$value) { while ($list = $value) { print $list['title']; print "<br>"; print $list['pages']; print "<br><br>"; } } else { echo "No Results Found"; } you can also have it like this but theres still more Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 30, 2007 Share Posted June 30, 2007 teng that only adds more unneeded parts to the script. Also since you using mysql_assoc you can just say in the while foreach($list as $key => $value) echo $key.": ".$value."<br/>"; then outside it echo another few breaks to separate it all out or do what you like. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 oops sorry i didnt see that any way i admit im wrong on that thanks Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 30, 2007 Share Posted June 30, 2007 you been schoolin' me all day so i gotta stab back once Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 30, 2007 Share Posted June 30, 2007 no i dont i told you im just bored at the office thats why Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.