Gavski Posted January 23, 2014 Share Posted January 23, 2014 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.<?phpfunction getsupp(){global $name,$address1,$address2,$address3,$address4,$pcode,$email;mysql_select_db("picture_cards");$result = mysql_query("SELECT *FROM supplierWHERE 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()?> Quote Link to comment https://forums.phpfreaks.com/topic/285609-problem-with-like-operator/ Share on other sites More sharing options...
smerny Posted January 23, 2014 Share Posted January 23, 2014 Can you give an example value of $name and what the resulting record's name field is? Quote Link to comment https://forums.phpfreaks.com/topic/285609-problem-with-like-operator/#findComment-1466272 Share on other sites More sharing options...
cyberRobot Posted January 23, 2014 Share Posted January 23, 2014 $result will only contain false if the query failed. Otherwise it returns a result set which will evaluate to true. Even if there were no matches for the query, it still returns a result set. The query was successful; there just weren't any matches. You could test if any rows were returned. if(mysql_num_rows($result)) { while($row = mysql_fetch_array($result)) { More information about mysql_num_rows() can be found here: http://www.php.net/manual/en/function.mysql-num-rows.php Quote Link to comment https://forums.phpfreaks.com/topic/285609-problem-with-like-operator/#findComment-1466282 Share on other sites More sharing options...
Barand Posted January 23, 2014 Share Posted January 23, 2014 DON'T use global. Have your function return the found results. Also you want to avoid other side effects of calling your function. If you have to select another database (other than the default) inside the function then you should restore the database connection before returning. e.g. function getsupp($name) { // get the current database $result = mysql_query("SELECT DATABASE()"); list($currentDB) = mysql_fetch_row($result); mysql_select_db("picture_cards"); $records = array(); $result = mysql_query("SELECT name, address1, address2, address3, address4, pcode, email FROM supplier WHERE name LIKE('$name%')") or die(mysql_error()); if($result ){ while($row = mysql_fetch_assoc($result)) { $records[] = $row; }//endwhile }//endif // restore current database connection mysql_select_db($currentDB); // return array of found records return $records; }// endfunc getsupp() $res = getsupp('fred'); if (res) { // process the array of results returned in $res } Quote Link to comment https://forums.phpfreaks.com/topic/285609-problem-with-like-operator/#findComment-1466293 Share on other sites More sharing options...
Solution Gavski Posted January 24, 2014 Author Solution Share Posted January 24, 2014 Thanks Cyber Robot, Managed to do this from your help and it works fine. $num_results = mysql_num_rows($result); if($num_results > 0) returns 0 if no records found. Also Barand, I have gone through my function and amended it (I kind of knew globals were a bad thing), now running like a well oiled address book, thanks for all help and tips Quote Link to comment https://forums.phpfreaks.com/topic/285609-problem-with-like-operator/#findComment-1466383 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.