PHP在函数格式中编写了like-statement?

时间:2015-05-05 00:33:32

标签: php mysql

我试图在函数格式中使用like语句。我使用mysql或pdo格式收到了很多结果,但它们对我已经设置的格式不起作用。它只是没有返回任何东西,我不确定这是否是正确的格式。这就是我所拥有的:

  function search_users($namesearch){
global $db;
    $query = "SELECT * FROM users 
            WHERE username LIKE '%:namesearch%'
                ORDER BY username";
    $statement = $db->prepare($query);
    $statement->bindValue(":namesearch", $namesearch);
    $statement->execute();
    $usersearch= $statement->fetchAll();
    $statement->closeCursor();
    return $usersearch;
}

1 个答案:

答案 0 :(得分:1)

我会这样做。

  function search_users($namesearch){
global $db;
    $query = "SELECT * FROM users 
            WHERE username LIKE ?
                ORDER BY username";
    $statement = $db->prepare($query);
    $statement->execute(array('%' . $namesearch . '%'));
    $usersearch= $statement->fetchAll();
    $statement->closeCursor();
    return $usersearch;
}