Reputation: 101
I have a php script that's called by AJAX and it keeps not returning the wildcard results I'd like to see. Can't figure out what's wrong. Odd bit is I'm trying to look up unusual characters in the DB like =.-,;,"
etc. The app I'm building is a English Character to Braille lookup.
Here's my code.
$ENG = "english phrase" // Could be Equal sign, dash whatever
$ENG_WILD = $ENG."%"; //Add in wildcard
$result = mysql_query("SELECT * FROM $DB_TABLE WHERE ENG LIKE '$ENG' OR '$END_WILD' ");
$row = mysql_fetch_array( $result );
$result = mysql_query("SELECT * FROM $DB_TABLE WHERE ENG LIKE '$ENG' OR '$END_WILD' ");
if (mysql_num_rows($result)==0) {
die($Failed . mysql_error());
}
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row
echo $box."Dots needed for..".$row['ENG']."..are..".$row['BR_PAR']."..The Rule that applies..".$row['RULE']."</p>".$box_end;
}
I've tried using *
instead of %
but it fails. Here's an example of an entry in the DB under ENG: = (Nemeth Code: Equal Sign)
If I run the script above with the input as a whole I get the proper result but if I run it with =
or = (Nemeth
I will get no results. I don't get it -- I would have thought the wildcard would have got me what I wanted. Thanks guys.
Upvotes: 1
Views: 210
Reputation: 780
SELECT * FROM $DB_TABLE WHERE ENG LIKE '$ENG' OR '$END_WILD'
Isn't probably doing what you think it is. With brackets, it's doing this:
SELECT * FROM $DB_TABLE WHERE (ENG LIKE '$ENG') OR ('$END_WILD')
When I think you want:
SELECT * FROM $DB_TABLE WHERE ENG LIKE '$ENG' OR ENG LIKE '$END_WILD'
It's also redundant to look for 'this' and 'this%', because the second will return 'this' as well.
You're also doing the exact same query twice for no apparent reason. I'm guessing there's lot of code we aren't seeing though.
Finally, having zero results shouldn't mean your query failed, there may just be no results for that query.
Upvotes: 0
Reputation: 1585
Your variable is $ENG_WILD
, but you're using $END_WILD
in your mysql_query()
function. (ENG vs END)
If you fix this and it's still not working, keep in mind that using %
at the end will only match if the phrase comes at the beginning of that field. Use a %
at both the beginning and end to match if it contains your phrase at the beginning, anywhere in the middle, or at the end.
Upvotes: 3