【发布时间】:2019-10-14 07:54:51
【问题描述】:
我关注PHP,通过在MARIADB 上执行SQL 为我提供数据。
function getData ($countsql, $datasql, $page, $limit, $input, $response){
try{
$offset = ($page-1) * $limit; //calculate what data you want
$db = new db();
$db = $db->connect();
$countQuery = $db->prepare( $countsql );
$dataQuery = $db->prepare( $datasql );
$dataQuery->bindParam(':limit', $limit, \PDO::PARAM_INT);
$dataQuery->bindParam(':offset', $offset, \PDO::PARAM_INT);
while(sizeof($input)){
$curr = array_pop($input);
$dataQuery->bindParam($curr["key"], $curr["keyvalue"]);
$countQuery->bindParam($curr["key"], $curr["keyvalue"]);
}
echo $datasql."+";
echo $limit."+";
echo $offset;
$dataQuery->execute();
$countQuery->execute();
$db = null; // clear db object
$count = $countQuery->fetch(PDO::FETCH_ASSOC);
$data = $dataQuery->fetchAll(PDO::FETCH_ASSOC);
if($count['COUNT']>0&&count($data)){
$data_arr=array();
$data_arr["records"]=array();
$data_arr["pagination"]=array();
$data_arr["records"] = $data;
$data_arr["pagination"] = array(
"count" => (int)$count['COUNT'],
"page" => $page,
"limit" => $limit,
"totalpages" => ceil($count['COUNT']/$limit)
);
return $response->withJson($data_arr,200);
}
else{
return $response->withJson (
array("msg" => "Nothing found."),
204
);
}
}catch( PDOException $e ) {
//return '{"error": {"msg":' . $e->getMessage() . '}';
return $response->withJson (
array("msg" => $e->getMessage()),
500
);
}
}
问题是当我在本地MARIADB 服务器上执行此操作时,代码工作正常,但是当我在线服务器上执行它时,它会产生以下错误(请注意,我最后打印了 SQL+limit+offset用于调试)
{"msg":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''999999' OFFSET 0' at line 1"}SELECT * , (SELECT COUNT(*) FROM ART WHERE ART.AUTHOR_ID = AUTHOR.ID) as COUNT FROM AUTHOR LIMIT :limit OFFSET :offset+999999+0
是否存在语法错误或版本不匹配?
编辑
我还想问一件小事,
这是获取 COUNT 的正确方法吗
SELECT * , (SELECT COUNT(*) FROM ART WHERE ART.AUTHOR_ID = AUTHOR.ID) as COUNT FROM AUTHOR
或者我应该加入这两个表然后计算相同吗?
【问题讨论】:
-
也许 MariaDB 不允许在
LIMIT中的数字加上引号?