Jump to content

PHP LIKE Statement


jmfillman

Recommended Posts

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
Share on other sites

"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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.