matty Posted March 2, 2007 Share Posted March 2, 2007 I want to have an IF statment which is something like this... $sel_names = mysql_query("select id, name users where userid='$userinfo[id]'"); $table = mysql_fetch_array($sel_names); If ( table[name] == "0") { echo "You have no data blah blah"; }else{ *Display a table with the data in* What i need to know is what do i use to find out if the table has any data in it Thanks, Matt Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/ Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 correct or not. <?php $sel_names = mysql_query("select id, name users where userid=' ".$userinfo['id']." ' "); $table = mysql_fetch_array($sel_names); while($data=mysql_fetch_assoc($table)){ if($data['what_ever']==0){ echo "You have no data blah blah"; }else{ //what ever } ?> Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197865 Share on other sites More sharing options...
chrisuk Posted March 2, 2007 Share Posted March 2, 2007 you could also use mysql_num_rows() eg: <?php $sql = "SELECT * FROM <table> where userid='$userinfo[id]'"; $result = mysql_query($sql) or die(mysql_error()); $numrows = mysql_num_rows($result); if ($num == 0) //no rows { echo "No Rows"; } else { *display* } ?> Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197869 Share on other sites More sharing options...
Snooble Posted March 2, 2007 Share Posted March 2, 2007 I think it's better using a varialbe that you ++. Example: <?php $count = 0; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) //echo table and results $count++; ?> <?php if ($count < 1) { echo "<br><br>No rows were found in this table"; } ?> Snooble Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197872 Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 snooble that code is slow hope you no that $count = 0; <<< wast time? while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) // a big mysql_fetch_assoc bad? //echo table and results $count++; another time waster always let mysql do the job if it can ok. ?> Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197873 Share on other sites More sharing options...
boo_lolly Posted March 2, 2007 Share Posted March 2, 2007 chrisuk had it right with mysql_num_rows(). that is what you want. with the if-statement just like he wrote, as well. that's how i'd do it anyway. Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197880 Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 boo_lolly there are no right's or wrongs can you show us the text on php where it say wright and wrong's please cheers lol Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197882 Share on other sites More sharing options...
boo_lolly Posted March 2, 2007 Share Posted March 2, 2007 boo_lolly there are no right's or wrongs can you show us the text on php where it say wright and wrong's please cheers lol what i meant was, it is exactly what he is looking for. he wanted to know how to check if there were any results returned (or how many) from a mysql query... mysql_num_rows does exactly that... no need to incriment or anything. it's simple, it works. that's what it's there for. that's why it's right. not that everyone elses is wrong. i never said that. but using mysql_num_rows is most right Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197888 Share on other sites More sharing options...
chrisuk Posted March 2, 2007 Share Posted March 2, 2007 Perhaps not "right or wrong" but the mysql_num_rows() function is specifically for this task, so why bother using anything else thats more complex and less efficient? The name of the function itself tells me that thats the best one to use Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197890 Share on other sites More sharing options...
Snooble Posted March 2, 2007 Share Posted March 2, 2007 I see, thanks for that redarrow. I didn't know which was faster. First table i wrote is the one i still use. cause i would echo out the variable I'll have a read up on mysql_num_rows() as i've never used it. Thanks! Snooble @ Chris. I'm sure you'll reflect on that statement. As you'll need to link some functions to get what you want sometimes. It helps to know how they work. Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197893 Share on other sites More sharing options...
redarrow Posted March 2, 2007 Share Posted March 2, 2007 In this post example your correct but as shown from above you dont always need to use mysql_num_rows but is best to but the same results are the same as posted on here ok. there are no right's or wrongs just preferences that why were all programmers. Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197896 Share on other sites More sharing options...
matty Posted March 2, 2007 Author Share Posted March 2, 2007 Thanks for sorting it guys, and offering alternative =p Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197908 Share on other sites More sharing options...
boo_lolly Posted March 2, 2007 Share Posted March 2, 2007 Thanks for sorting it guys, and offering alternative =p dirka Link to comment https://forums.phpfreaks.com/topic/40867-solved-determining-if-a-table-has-0-rows/#findComment-197990 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.