Page 1 of 1

Problem with 'LIKE' operator

Posted: Thu Jan 23, 2014 5:42 am
by Gavski1
This is a function that searches an address DB, compares the $name with 'LIKE' and returns the address details.
If no address is found, I want to go to another function.

In my form, if I leave $name blank it works, but if I put a $name that is not in the database, $result still returns a value and doesn't seem to recognize that there is no record??

any ideas, thanks.

Code: Select all

<?php

function getsupp()
{
global $name,$address1,$address2,$address3,$address4,$pcode,$email;

mysql_select_db("picture_cards");
	
	$result = mysql_query("SELECT *
     FROM supplier
    WHERE name LIKE('$name%')") or die(mysql_error());

    if($result ){
    while($row = mysql_fetch_array($result))
       {
           $name = $row['name'];
           $address1 = $row['address1'];
           $address2 = $row['address2'];
           $address3 = $row['address3'];
           $address4 = $row['address4'];
           $pcode = $row['pcode'];
           $email = $row['email'];
   
       }//endwhile
     
	 
	 }//endif
}// endfunc getsupp()

 ?>

Re: Problem with 'LIKE' operator

Posted: Thu Jan 23, 2014 6:47 am
by Celauran
What's being passed in? What's being returned?

Re: Problem with 'LIKE' operator

Posted: Thu Jan 23, 2014 7:25 am
by Gavski1
$name is being passed, if a record 'LIKE' $name id found in the DB it returns all the other values $address1 '.....etc.
Its like a shortcut to fill in an address - so you only have to enter the $name and the rest of the address details will be filled in.

Unfortunately if DB is queried for any $name 'LIKE', $return is seen as being true.
I need $return to return some false or NULL value (seems to happen ok if $name is empty)?

cheers

Re: Problem with 'LIKE' operator

Posted: Thu Jan 23, 2014 8:59 am
by Celauran
Gavski1 wrote:$name is being passed
Yes, I can see that. What I'm asking is to echo the parsed query as well as its results to maybe gain some insight as to what's going on. At a glance, the code looks like it should work.

Re: Problem with 'LIKE' operator

Posted: Fri Jan 24, 2014 5:19 am
by Gavski1
I found out that querying the db will return a value for $result even if a record is not present. What I needed was

Code: Select all

 

$num_results = mysql_num_rows($result);

if($num_results > 0)

returns 0 if no records found.

Thanks for your help