简体   繁体   中英

MySQL LIKE query is not worked for search record with apostrophes

when i start searching a record from db i got an issue when apostrophes present in word for that i used addslashes,mysql_real_escape_strin but not worked for that

<?php
    include("lib/dbconn.php");
    $list_query_main1="select * from table where name like '%".mysql_real_escape_string($_REQUEST['keyword'])."%'";
   $list=mysql_query($list_query_main1);
   echo mysql_num_rows($list);
?>

Zero results found but name present in DB give me solution.

you are getting mysql error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table' at line 1

because TABLE is reserved word. If you named your table like TABLE, you must use right mysql syntax

$list_query_main1="select * from `table` where `name` like '%".mysql_real_escape_string($_REQUEST['keyword'])."%'";

Adding if(!$list || mysql_errno() != 0) echo mysql_error(); after line $list=mysql_query($list_query_main1); will give you some info in case of a query failure

Otherwise myqsl_* is deprecated you should start using mysqli_* functions.

And change your last line

echo $mysql_num_rows($list);

and replace it with

echo mysql_num_rows($list);

If you want to call myqsl_num_rows() function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM