简体   繁体   English

php / sql-如何在变量上使用LIKE运算符从数据库中提取结果?

[英]php/sql- How do I use LIKE operator on variable to pull results from database?

As question said, I want to use the LIKE Operator so whenever user inputs something, like "M", it returns all database entries starting with "m" rather then anything named just "M". 就像我说的那样,我想使用LIKE运算符,因此无论何时用户输入“ M”之类的东西,它都会返回所有以“ m”开头的数据库条目,而不是仅以“ M”开头的所有条目。 This is my code 这是我的代码

$strSymbolName = @$_REQUEST["searchSymbol"];
//searchSymbol is the variable with user Input inside

if(!empty($strSymbolName))
{ // process the form
 // Establish dbserver connection and default database

$db = $objDBUtil->Open();
 // Run a Query to get company name

$query = "SELECT symSymbol, symName FROM symbols " .
 "WHERE symSymbol =" . $objDBUtil->DBQuotes($strSymbolName);
//Retrieves company symbol and name from database. Stores this in result

$result = $db->query($query);`

What I thought of doing was using the LIKE operator on the WHERE statement, so something like- 我想做的是在WHERE语句上使用LIKE运算符,所以类似-

WHERE symSymbol LIKE = " . $objDBUtil->DBQuotes($strSymbolName);

But this didn't work...How would I apply the LIKE operator here? 但这没有用...我如何在这里应用LIKE运算符?

$db = $objDBUtil->Open();

$param = $objDBUtil->DBQuotes($strSymbolName);

$query = "SELECT symSymbol, symName FROM symbols WHERE symSymbol LIKE '%$param%'";

$result = $db->query($query);

just use LIKE query 只需使用LIKE查询

$db = $objDBUtil->Open();
$query = "SELECT symSymbol, symName FROM symbols WHERE symSymbol LIKE '%$strSymbolName%'";
$result = $db->query($query);

You need to append the % on the end of your variable like: 您需要在变量末尾附加% ,例如:

WHERE symSymbol LIKE " . $objDBUtil->DBQuotes($strSymbolName) . "%";

This will return anything starting with your variable. 这将返回以您的变量开头的任何内容。

If you want to search something that starts with "M", add a % after the value, and remove the = as it is not needed when using LIKE 如果要搜索以“ M”开头的内容,请在值后添加% ,并删除=因为使用LIKE时不需要

$query = "SELECT symSymbol, symName FROM symbols WHERE symSymbol LIKE ".$objDBUtil->DBQuotes($strSymbolName)."%";

For more information about LIKE , check here . 有关LIKE更多信息,请单击此处

WHERE CustomerName LIKE 'a%' || WHERE CustomerName喜欢'a%'|| Finds any values that starts with "a" 查找以“ a”开头的任何值

WHERE CustomerName LIKE '%a' || WHERE CustomerName像'%a'|| Finds any values that ends with "a" 查找以“ a”结尾的任何值

WHERE CustomerName LIKE '%or%' || WHERE CustomerName喜欢'%or%'|| Finds any values that have "or" in any position 查找在任何位置具有“或”的任何值

WHERE CustomerName LIKE '_r%' || WHERE CustomerName喜欢'_r%'|| Finds any values that have "r" in the second position 查找第二个位置具有“ r”的任何值

WHERE CustomerName LIKE 'a_%_%' || WHERE CustomerName喜欢'a _%_%'|| Finds any values that starts with "a" and are at least 3 characters in length 查找以“ a”开头且长度至少为3个字符的任何值

WHERE ContactName LIKE 'a%o' || 其中的ContactName喜欢'a%o'|| Finds any values that starts with "a" and ends with "o" 查找以“ a”开头并以“ o”结尾的任何值

Use the % wildcard after the string you want to look for. 要查找的字符串后使用%通配符。

$query = "SELECT symSymbol, symName FROM symbols WHERE symSymbol LIKE ".$objDBUtil->DBQuotes($strSymbolName)."%";

A bit more on the % and other wildcards: https://www.w3schools.com/sql/sql_wildcards.asp 关于%和其他通配符的更多信息: https : //www.w3schools.com/sql/sql_wildcards.asp

Excerpt from above link: 摘录自以上链接:

WHERE CustomerName LIKE 'a%' || WHERE CustomerName喜欢'a%'|| Finds any values that starts with "a" 查找以“ a”开头的任何值

WHERE CustomerName LIKE '%a' || WHERE CustomerName像'%a'|| Finds any values that ends with "a" 查找以“ a”结尾的任何值

WHERE CustomerName LIKE '%or%' || WHERE CustomerName喜欢'%or%'|| Finds any values that have "or" in any position 查找在任何位置具有“或”的任何值

WHERE CustomerName LIKE '_r%' || WHERE CustomerName喜欢'_r%'|| Finds any values that have "r" in the second position 查找第二个位置具有“ r”的任何值

WHERE CustomerName LIKE 'a_%_%' || WHERE CustomerName喜欢'a _%_%'|| Finds any values that starts with "a" and are at least 3 characters in length 查找以“ a”开头且长度至少为3个字符的任何值

WHERE ContactName LIKE 'a%o' || 其中的ContactName喜欢'a%o'|| Finds any values that starts with "a" and ends with "o" 查找以“ a”开头并以“ o”结尾的任何值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM