PHP / PDO搜索模式匹配

when i search for a user in database, i do:

$result = $dbh->query("SELECT user FROM table_user WHERE user = '".$user."' ");
    $result->execute();
    while ($user = $result->fetch(PDO::FETCH_ASSOC)) {
        $array[] = $user['user'];
    }

But i first get a result, when

user

and

$user

are exactly the same.

But i already need a result, when "$user" is part of "user", anybody knows how to do such a pattern matching?

Greetings!!

You are searching for the LIKE statement. Further note that when you interpolate $user this way you are vulnerable against SQL injections. You should use a prepared statement-

Use this:

$stmt = $dbh->prepare("SELECT user FROM table_user WHERE user LIKE :search");
$stmt->execute(array('user' => "%$user%"));

while($user = $stmt->fetch()) {
    var_dump($user);
}