我有以下代码:
$countQuery = "SELECT ARTICLE_NO FROM ? WHERE upper(ARTICLE_NAME) LIKE '% ? %'"; if ($numRecords = $con->prepare($countQuery)) { $numRecords->bind_param("ss", $table, $brand); $numRecords->execute(); $data = $con->query($countQuery) or die(print_r($con->error)); $rowcount = mysql_num_rows($data); $rows = getRowsByArticleSearch($query, $table, $max); $last = ceil($rowcount/$page_rows); }
哪个应该工作正常.但是我收到的错误是:
您的SQL语法有错误; 检查与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法?WHERE upper(ARTICLE_NAME)LIKE'%?%''在第1行
如果我放
SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE '% o %';
查询工作正常.$ table在上面定义,并且从GET接收查询,并且两者都是正确的有效值.为什么这会失败?
编辑:更改为:
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS1 WHERE upper(ARTICLE_NAME) LIKE '% ? %'"; if ($numRecords = $con->prepare($countQuery)) { $numRecords->bind_param("s", $query);
导致错误:
警告:mysqli_stmt :: bind_param()[mysqli-stmt.bind-param]:变量数量与C:\ Program Files\EasyPHP 3.0\www\prog\get_records.php中预准备语句中的参数数量不匹配38
命令不同步; 你现在不能运行这个命令
在哪里
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS1 WHERE upper(ARTICLE_NAME) LIKE ?"; if ($numRecords = $con->prepare($countQuery)) { $numRecords->bind_param("s", "%".$query."%");
结果是
致命错误:无法在第38行的C:\ Program Files\EasyPHP 3.0\www\prog\get_records.php中通过引用传递参数2
最后
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS1 WHERE upper(ARTICLE_NAME) LIKE ? "; if ($numRecords = $con->prepare($countQuery)) { $numRecords->bind_param("s", $query);
只会给:
命令不同步; 你现在不能运行这个命令
是不是可以使用一个参数来制作一个LIKE statament?
对于LIKE
子句,使用此:
SELECT ARTICLE_NO FROM AUCTIONS1 WHERE upper(ARTICLE_NAME) LIKE CONCAT('%', ?, '%')
至于表名,将表名作为参数是一种非常糟糕的做法.
如果由于某种原因您仍需要执行此操作,则需要在准备查询之前将其嵌入查询文本中:
$countQuery = "SELECT ARTICLE_NO FROM $table_name WHERE upper(ARTICLE_NAME) LIKE CONCAT('%', ? ,'%')"; if ($numRecords = $con->prepare($countQuery)) { $numRecords->bind_param("s", $brand); $numRecords->execute(); $data = $con->query($countQuery) or die(print_r($con->error)); $rowcount = mysql_num_rows($data); $rows = getRowsByArticleSearch($query, $table, $max); $last = ceil($rowcount/$page_rows); }