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); } Quote Link to comment 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. Quote Link to comment 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.