如何返回string是属性的子串的所有行

时间:2015-01-14 15:33:26

标签: php mysql

我在我的网站上搜索过用户。我想返回名称中包含输入字符串或等于字符串的所有用户行。

它看起来像这样:

$name = $_POST['name'];    
"SELECT * FROM users WHERE name='$name' OR **'$name' is substr of name**"

1 个答案:

答案 0 :(得分:1)

使用LIKE

$name = $_POST['name'];
$query = "SELECT * FROM users WHERE name LIKE '%$name%'";

使用PDO:

$name = $_POST['name'];
try 
{
    $dbh = new PDO("mysql:host=YourHost;dbname=YourDatabase", $username, $password);    
    $stmt = $dbh->query("SELECT * FROM users WHERE name LIKE '%$name%'");    

    //fetch your data.....   

    $dbh = null;
}
catch(PDOException $e)
{
    echo $e->getMessage();
}