Lamez Posted February 27, 2008 Share Posted February 27, 2008 is there a way in PHP to check to see if a row has anything in it? I making a function, and I want it to check to see if there is anything in that row. if($session->isAdmin()){ function barView($var, $table){ $q = "Select `$var` From `$table`"; mysql_query($q); } Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/ Share on other sites More sharing options...
trq Posted February 27, 2008 Share Posted February 27, 2008 Run your query and check mysql_num_rows to find out if any rows where returned. Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477720 Share on other sites More sharing options...
priti Posted February 27, 2008 Share Posted February 27, 2008 also you can try to check the resource type if it's empty. $result=mysql_query($q); if(empty($result)) or you may be checking $result == flase.... i am not sure which one is correct kindly try and see whether its useful or not. regards Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477723 Share on other sites More sharing options...
trq Posted February 27, 2008 Share Posted February 27, 2008 also you can try to check the resource type if it's empty. $result=mysql_query($q); if(empty($result)) or you may be checking $result == flase.... i am not sure which one is correct kindly try and see whether its useful or not. regards That will only show that the query was successful, not if it returned any results. There is a difference. Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477724 Share on other sites More sharing options...
Lamez Posted February 27, 2008 Author Share Posted February 27, 2008 all I want to know if there is somthing in there or not, and if so then do not allow a drop down. Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477741 Share on other sites More sharing options...
trq Posted February 27, 2008 Share Posted February 27, 2008 Your question is pretty unclear. Do you want to know if the row exists? Or wether a particular field has a value. Either way, mysql_num_rows is your friend. Check to see if a row with an id of 1 exists. <?php $sql = "SELECT foo FROM tbl WHERE id = 1"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // row exists. } else { // row does not exist } } ?> Check if the orw with an id of 1 contains a value in field foo. <?php $sql = "SELECT foo FROM tbl WHERE id = 1 && foo <> ''"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // row exists. } else { // row does not exist } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477745 Share on other sites More sharing options...
Lamez Posted February 27, 2008 Author Share Posted February 27, 2008 I got it, thanks to some logical thinking, and the mysql_num_rows here is my function: <?php function barView($var, $table){ $num = mysql_num_rows(mysql_query("Select `$var` From `$table`")); if ($num >= 1){ echo "there is somthing in the table"; }else{ echo "nothing in the table"; } } ?> in my other page: <?php barView(a1, rnd1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477749 Share on other sites More sharing options...
trq Posted February 27, 2008 Share Posted February 27, 2008 Much better coded as.... <?php function barView($var, $table) { $sql = "SELECT `$var` FROM `$table`"; if ($result = mysql_query($sql)) { return mysql_num_rows($result); } return false; } if (barView('a1', 'rnd1')) { echo "there is somthing in the table"; } else { echo "nothing in the table"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477756 Share on other sites More sharing options...
Lamez Posted February 27, 2008 Author Share Posted February 27, 2008 why is that better? I would like to learn how to make a much better, and more stable code. Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477760 Share on other sites More sharing options...
trq Posted February 27, 2008 Share Posted February 27, 2008 In your code, this line here.... $num = mysql_num_rows(mysql_query("Select `$var` From `$table`")); Is vulnerable to errors. You make no attempt to check that mysql_query() returns a result resource before using it in mysql_num_rows(). mysql_num_rows() expects a result resource, anything else will make it throw an error. As for the rest, I simply removed the checking logic into the calling code. Notice that most php functions return true or false? So should yours. This makes your functions allot more usable in different situations. I very rarely have a function echo anything. Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477762 Share on other sites More sharing options...
Lamez Posted February 27, 2008 Author Share Posted February 27, 2008 Well noted, now I will revise and edit. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/93268-mysql-check-for-any-value/#findComment-477765 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.