bschultz Posted June 30, 2011 Share Posted June 30, 2011 The following code isn't working...and I don't know why... <?php $dbc = mysql_pconnect($host, $username, $password); mysql_select_db($db,$dbc); $sql = "SELECT * FROM ump_names WHERE `association_id` = '$_SESSION[association_id]' AND `is_active` = '1' AND `last_name` LIKE '$_GET[first]%' order by last_name ASC"; //echo "$sql<br />"; $rs = mysql_query($sql,$dbc); if (!$rs) { echo "There are no matches for this letter..."; } while($row = mysql_fetch_array($rs)) { echo "<div class='style5'><strong>$row[first_name] $row[last_name]</strong><br />"; echo "$row[address]<br />"; echo "$row[city], $row[state] $row[zip]<br />"; echo "Home - $row[home_number]<br />"; echo "Cell - $row[cell_number]<br />"; echo "Work - $row[work_number]<br />"; echo "<a href='mailto:$row[email]'>$row[email]</a></div><br />------------------------<br />"; } ?> If there are matches with $_GET[first], everything works great...if there are no matches, there's no error (error reporting is turned on ) and the line There are no matches for this letter... never is echoed. Any help on what I'm doing wrong...??? Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 Are you sure it isn't returning an empty results set? If you echo the query string and paste it into phpMyAdmin (or similar), what results do you get? Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/#findComment-1236640 Share on other sites More sharing options...
PFMaBiSmAd Posted June 30, 2011 Share Posted June 30, 2011 A query that executes without any database errors, even if the query matches zero rows, is a successful query and your $rs variable will contain a result resource. You would need to use mysql_num_rows() to find out how many rows the result set contains. Your existing code will only echo the There are no matches for this... message if the query failed due to an error of some kind (connection problem, sql syntax error, spelling/typo in the table/column names, ...) Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/#findComment-1236641 Share on other sites More sharing options...
bschultz Posted June 30, 2011 Author Share Posted June 30, 2011 MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0005 sec ) if (mysql_num_rows($rs)==0) { echo "There are no matches for this letter..."; }...worked...thanks! Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/#findComment-1236642 Share on other sites More sharing options...
bschultz Posted June 30, 2011 Author Share Posted June 30, 2011 Is it returning an empty set because of LIKE? If it were asking for an absolute match, would it throw an error? Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/#findComment-1236643 Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 It's returning an empty results set because there are no records that match all the conditions in the WHERE clause. Did you echo the query string to see if it contains the values you'd expect it to contain? Does the $_SESSION var have a value? I don't see a call to session_start() in the code. Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/#findComment-1236653 Share on other sites More sharing options...
bschultz Posted June 30, 2011 Author Share Posted June 30, 2011 It's working just fine with number_rows...I was just curious as to why it returned an empty set the other way. Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/#findComment-1236661 Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 It's returning an empty results set both ways. A query will execute without error even if no results are returned, so you can't rely on if( mysql_query() ) to determine if there were results returned, only to see if the query produced an error condition. Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/#findComment-1236662 Share on other sites More sharing options...
bschultz Posted June 30, 2011 Author Share Posted June 30, 2011 ok...thanks Quote Link to comment https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/#findComment-1236663 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.