开发者

Mysql query should produce results, but doesn't

开发者 https://www.devze.com 2023-04-05 04:24 出处:网络
I\'m working on a new search script for my website that contains multiple dictionaries. First let me show you the problem piece of code and then explain....

I'm working on a new search script for my website that contains multiple dictionaries. First let me show you the problem piece of code and then explain....

$sql = mysql_query("SELECT * FROM $tbl_name WHERE $field = '%$trimmed%' ORDER BY $field ".$sort." LIMIT ".$limits.", $max")or die(mysql_error());
$count = mysql_result(mysql_query("SELECT COUNT($field) FROM $tbl_name WHERE $field = '%$trimmed%'"),0);

if ($count < 1){

$sql = mysql_query("SELECT * FROM $tbl_name WHERE $field LIKE '%$trimmed%' ORDER BY $field ".$sort." LIMIT ".$limits.", $max")or die(mysql_error());
$count = mysql_result(mysql_query("SELECT COUNT($field) FROM $tbl_name WHERE $field LIKE '%$trimmed%'"),0);

}

Okay, in theory, the first query should select results that are exact and display them without displaying the "LIKE" results. However, it does not do that. It simply always shows the LIKE results.

And when I remove all of that, and leave the first query - no results are returned - even though they are in the database.

For example, with everything after "if ($count <1 ) {" included, I can se开发者_如何学JAVAarch "SHE" or "I" and get results - however it includes words like "informal", "singular", etc. But when I remove this, I can search "SHE" and "I" and get no result at all, even though they're in the database.

Any help would be greatly appreciated.


Remove the '%' characters around "$trimmed" in the first query.

See if that fixes it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号