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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.