使用LIKE子句时mysqli准备问题? PHP

时间:2013-01-26 04:05:18

标签: php mysqli

我正在尝试从数据库中选择前10个结果,其中fullName LIKE'Ma%'

它一直没有返回结果。我100%确定表中有一些结果,因为当我运行相同的查询而不使用mysqli时它可以工作!

这段代码发生了什么?有没有让这个代码运行的技巧?

这是我到目前为止所拥有的

$term = "'Ma%'";


$query = $db->prepare('SELECT customerID, fullName, birthYear, homeAddress, ID, DATE_FORMAT(idIssue, "%d-%m-%Y") AS idIssue, DATE_FORMAT(idExp, "%d-%m-%Y") AS idExp, phone
                       FROM customers WHERE (fullName LIKE ? ) LIMIT 0,10');
$query->bind_param('s', $term);

$query->execute();

1 个答案:

答案 0 :(得分:3)

$term = "Ma%"; // <-- remove the extra quotes

$sql = 'SELECT customerID, fullName, birthYear, homeAddress, ID, phone, 
               DATE_FORMAT(idIssue, "%d-%m-%Y") AS idIssue, 
               DATE_FORMAT(idExp, "%d-%m-%Y") AS idExp
        FROM customers WHERE fullName LIKE ? LIMIT 0,10'
$query = $db->prepare($sql);
$query->bind_param('s', $term);
// s refers to a string, i for an int... etc.
$query->execute();
// I hope you have some code below this line to actually display the returned data

如果您对此查询有任何其他问题,则它们与您发布的代码无关。检查拼写错误,正确凭据,正确保存的文件和实际数据。