-1

I need help to filter column with type "LONGTEXT" using LIKE within php statement. Text I want to filter contain "HIL"

Already tried using mysql_real_escape_string('HIL') but still error, below is the code i am using now:

$umr_query=sprintf("SELECT * FROM customers_umr WHERE umrCat LIKE '% HIL %'");

expected php able to show sorted mySQL table, as tested in SQL query

SELECT * FROM customers_umr WHERE umrCat LIKE '% HIL %' 

that this query works

Thank you

Qirel
  • 21,424
  • 7
  • 36
  • 54
  • 1
    How do you execute this query in PHP? – Qirel Jul 18 '19 at 09:40
  • All you have done is put characters into a string variable. You have not passed that string to the database to be executed. – RiggsFolly Jul 18 '19 at 09:51
  • 1
    You should also be getting a warning `Warning: sprintf(): Too few arguments in` HINT: you dont need to us `sprintf()` in this situation – RiggsFolly Jul 18 '19 at 09:52
  • You probably need to read [the PDO section of the PHP manual](https://www.php.net/manual/en/book.pdo.php) And/Or a few PHP/MySQL tutorials – RiggsFolly Jul 18 '19 at 09:53
  • _expected php able to show sorted mySQL table_ Another Hint: If you want the result (once you get one) to be sorted then you also need to lookup `ORDER BY` in the MySQL manual – RiggsFolly Jul 18 '19 at 10:02
  • 1
    Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Jul 18 '19 at 10:03

1 Answers1

-1

This query is searching for " HIL " not "HIL"

$umr_query=sprintf("SELECT * FROM customers_umr WHERE umrCat LIKE '% HIL %'");

remove the spaces

$umr_query=sprintf("SELECT * FROM customers_umr WHERE umrCat LIKE '%HIL%'");
John.M
  • 296
  • 1
  • 7
  • 1
    There is no real reason to assume that `LIKE '% HIL %'` is wrong, specially as the OP says the ran that in that state somehow anyway! There are so many other errors in the script anyway – RiggsFolly Jul 18 '19 at 10:04
  • OP says: Text I want to filter contain "HIL" - His query searches for " HIL " - it's very clearly wrong and your downvote is unwarranted. – John.M Jul 18 '19 at 10:24
  • So what if the text the OP is looking for is `The HIL are alive with the sound of music` – RiggsFolly Jul 18 '19 at 10:27
  • It's clear to me .. you seem confused, perhaps you should ask. – John.M Jul 18 '19 at 10:32
  • 1
    Afterall its not even the most obvious error in that code. The `sprintf()` will generare a warning, there appears to be no attempt to actually pass that query (right or wrong) to the databsase i.e. `query()` or `execute()` etc etc – RiggsFolly Jul 18 '19 at 10:33
  • Once the query is passed to the db though it won't find what he is looking for. – John.M Jul 18 '19 at 10:34
  • Or will it. Like the OP said, they tested it presumably in phpMyAdmin or something similiar – RiggsFolly Jul 18 '19 at 10:34