0

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?

3
  • "LIKE %".$objDBUtil->DBQuotes($strSymbolName)."%" Commented Jun 8, 2017 at 2:40
  • Get rid of the = sign after LIKE and then use % wildcard markers where appropriate. Commented Jun 8, 2017 at 2:40
  • I tried that line, replaced it with what I showed above, but my program isn't recognizing it (the line is gray) "WHERE symSymbol "LIKE %".$objDBUtil->DBQuotes($strSymbolName)."%" Commented Jun 8, 2017 at 3:30

5 Answers 5

1
$db = $objDBUtil->Open();

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

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

$result = $db->query($query);
Sign up to request clarification or add additional context in comments.

3 Comments

Welcome to SO. Can you also put in some explanation of what the code does and format it correctly please?
$db = $objDBUtil->Open(); $param = $objDBUtil->DBQuotes($strSymbolName); $query = "SELECT symSymbol, symName FROM symbols WHERE symSymbol LIKE '%$param%' "; $result = $db->query($query);
I meant in your answer. Format the code using the code tags and put in some explanation for users as well please
1

just use LIKE query

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

Comments

0

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"

Comments

0

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"

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.