jmfillman Posted November 15, 2006 Share Posted November 15, 2006 I'm quite new to PHP and trying to understand and modify some sample code I received to include a LIKE search. This code always returns the entire "users" table. Everything but this piece of the code [b][color=red]LIKE '%$ptSearchFirstName%'[/color][/b] is exactly the same as the working sample code I received. public function ptSearch() { # Return a list of all the 'flextest' users if (!$result=$this->mysqli->query("SELECT * from users WHERE username LIKE '%$ptSearchFirstName%'")) { $msg=$this->err_prefix."SELECT query error: ".$this->mysqli->error; $this->mysqli->close(); throw new Exception($msg); } while ($row = $result->fetch_assoc()) { $user_array[] = $row; } return($user_array); } Link to comment https://forums.phpfreaks.com/topic/27308-php-like-statement/ Share on other sites More sharing options...
btherl Posted November 15, 2006 Share Posted November 15, 2006 "WHERE username LIKE '%$ptSearchFirstName%'" will give you all users whose username has $ptSearchFirstName as a substring. The '%' means "match any string". So it translates to "Match anything followed by $ptSearchFirstName followed by anything".It's actually Mysql code, not php code, but mysql is used often with php.If $ptSearchFirstName is empty, then it will return all users, since it'll just be "LIKE '%%'", and % matches anything. Link to comment https://forums.phpfreaks.com/topic/27308-php-like-statement/#findComment-124866 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.