Jump to content

mysql query no matches not working


bschultz

Recommended Posts

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...???

Link to comment
https://forums.phpfreaks.com/topic/240758-mysql-query-no-matches-not-working/
Share on other sites

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, ...)

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.