Hi there. This prints 'Edit Album' even when i enter something that's not in the database (or anything that's not LIKE something in the database):

unset($result);
$result = mysql_query("SELECT artist, title, ID FROM albums WHERE artist LIKE '$artist%' AND title LIKE '$title%'") or
	die("Could not search for album");

// Check that there were results
if(!$result)
{
     die("<h3>No results in search.</h3>  " . mysql_error());
}
else	//there is a match
{
     echo '<h3>Edit Album:</h3>';
}

Does anyone know why? Thanks.

    How do you set the $artist and $title variables? If they are empty strings, it will always match.

    If you have these problems, ALWAYS echo out the query string to see what is happening!

    J

      I set them like this(from a form):

      $artist = mysql_real_escape_string($_POST['artist']);
      $title = mysql_real_escape_string($_POST['title']);

      I check to make sure they aren't empty before I do the query.

      I've tried echo'ing out $result and print_r(mysql_fetch_array($result)) and get nothing.

        An empty result set does not cause mysql_query() to return false. If the query was valid, it will still return a result set resource ID. If you want to check for no matches, do a [man]mysql_num_rows/man to find out how many rows were returned by your query (including a 0 rows result).

          Write a Reply...