Reputation: 23
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". 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 symSymbol LIKE = " . $objDBUtil->DBQuotes($strSymbolName);
But this didn't work...How would I apply the LIKE operator here?
Upvotes: 0
Views: 411
Reputation: 124
just use LIKE query
$db = $objDBUtil->Open();
$query = "SELECT symSymbol, symName FROM symbols WHERE symSymbol LIKE '%$strSymbolName%'";
$result = $db->query($query);
Upvotes: 1
Reputation: 11
$db = $objDBUtil->Open();
$param = $objDBUtil->DBQuotes($strSymbolName);
$query = "SELECT symSymbol, symName FROM symbols WHERE symSymbol LIKE '%$param%'";
$result = $db->query($query);
Upvotes: 1
Reputation: 7078
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.
Upvotes: 0
Reputation: 1027
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
Excerpt from above link:
WHERE CustomerName LIKE 'a%' || Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' || Finds any values that ends with "a"
WHERE CustomerName LIKE '%or%' || Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' || Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%_%' || Finds any values that starts with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' || Finds any values that starts with "a" and ends with "o"
Upvotes: 0
Reputation: 5401
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
$query = "SELECT symSymbol, symName FROM symbols WHERE symSymbol LIKE ".$objDBUtil->DBQuotes($strSymbolName)."%";
For more information about LIKE
, check here.
WHERE CustomerName LIKE 'a%' || Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' || Finds any values that ends with "a"
WHERE CustomerName LIKE '%or%' || Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' || Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%_%' || Finds any values that starts with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' || Finds any values that starts with "a" and ends with "o"
Upvotes: 0