MySQL/PHP LIKE with percent signs not working
I am aware that this question has been asked several times before, but I am unable to find the error of my ways.
开发者_如何学编程The question: mysql_num_rows is returning false results when
$sql = "SELECT * FROM $topic WHERE $names LIKE '%$q%'";
But if I replace $sql with any of the following, it will return true.
$sql = "SELECT * FROM $topic WHERE $names LIKE '%j%'";
$sql = "SELECT * FROM $topic WHERE $names ='Jack'";
$sql = "SELECT * FROM $topic WHERE $names = '$q' ";
Results of var_dump
string(8) "Cust_Reg" string(5) "fName" string(2) "j "
resource(8) of type (mysql result)
string(46) "SELECT * FROM Cust_Reg WHERE fName LIKE '%j %'"
If I change $sql = "SELECT * FROM $topic WHERE $names LIKE '%j%'";
var_dump for $row['ID'] will display
"SELECT * FROM Cust_Reg WHERE fName LIKE '%j%'" string(4) "NjA="
string(4) "NjE=" string(4) "NjQ=" string(4) "NjY=" string(4) "ODI="
If you could correct me in the error of my way, I will apprecaite it.
$q = mysql_real_escape_string($_GET['search']);
$q = strtolower($q);
$topic = mysql_real_escape_string($_GET['test']);
$names = mysql_real_escape_string($_GET['name']);
// SELECT * from Account_Reg where Account_Name LIKE '%$q%'
$table = "<table style='width:400px; padding:10; display:block;'><tbody>
<tr><td>ID</td><td>Account</td><td>First Name</td><td>Email</td></tr>";
$sql = "SELECT * FROM $topic WHERE $names LIKE '%{$q}%'";
$result = mysql_query($sql) or die (mysql_error());
var_dump($topic);
var_dump($names);
var_dump($q);
var_dump($result);
var_dump($sql);
if(is_resource($result) && mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)) {
$table .= "<tr><td>".$row['ID']."</td>";
$cryt = base64_encode($row['ID']);
$row['ID'] = htmlspecialchars($cryt);
$link = "profile.cust.update.php?id=". urlencode($row['ID']);
$link = htmlentities($link);
if($names == "fName"){
$name = $row['fName'];
}elseif($names == "Account_Name"){
$name = $row['Account_Name'];
$row['email_add'] = "";
}
$table .="<td></td><td><a href='" .$link ."'</a>" .$name."</td><td>".$row['email_add']."</td></a> </tr>";
}$table .="</tbody</table";
}else{$table = "No row is selected"; }
try using:
$sql = "SELECT * FROM $topic WHERE $names LIKE '%{$q}%'";
or
$sql = "SELECT * FROM $topic WHERE $names LIKE '%" . $q . "%'";
and for debug, try to output $sql before executing, to see how the variables have been replaced
echo $sql; die();
@AdamWaite from Documentation:
http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html
MySQL provides standard SQL pattern matching as well as a form of pattern matching based on extended regular expressions similar to those used by Unix utilities such as vi, grep, and sed.
SQL pattern matching enables you to use “_” to match any single character and “%” to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-insensitive by default. Some examples are shown here. You do not use = or <> when you use SQL patterns; use the LIKE or NOT LIKE comparison operators instead.
The problem is that trailing whitespace in your $q "j "
. Of course
"SELECT * FROM $topic WHERE $names LIKE '%$q%'"
which translates to:
"SELECT * FROM $topic WHERE $names LIKE '%j %'"
returns different results from:
"SELECT * FROM $topic WHERE $names LIKE '%j%'"
精彩评论