Php PDO向绑定参数添加单引号
我正在使用Slim框架为站点构建一个RESTful后端,使用PHP的PDO查询数据库。但是,当我尝试将参数绑定到准备好的语句时,会出现以下错误: 字符串(206)“SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解第1行“10”附近使用的正确语法”` 看起来PDO用单引号围绕着我的绑定参数。我的路线代码如下:Php PDO向绑定参数添加单引号,php,mysql,pdo,slim,Php,Mysql,Pdo,Slim,我正在使用Slim框架为站点构建一个RESTful后端,使用PHP的PDO查询数据库。但是,当我尝试将参数绑定到准备好的语句时,会出现以下错误: 字符串(206)“SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解第1行“10”附近使用的正确语法”` 看起来PDO用单引号围绕着我的绑定参数。我的路线代码如下: $app->get("/members/next10/:offset", function($offse
$app->get("/members/next10/:offset", function($offset) use($app) {
$app->response()->header('Content-Type', 'application/json');
try {
$pdo = connectToDatabase();
$query = $pdo->prepare("SELECT * FROM `members` LIMIT 10 OFFSET :offset");
$query->bindParam(":offset", $offset, PDO::PARAM_INT);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
if (!empty($result)) {
echo json_encode($result);
} else {
$app->response()->status(404);
}
} catch (PDOException $d) {
$app->response()->status(500);
var_dump($d->getMessage());
}
});
我错过什么了吗?我已尝试从URL中获取
:offset
,并在绑定之前将其分配给$offset
强制转换为整数,但这也没有什么区别。可能的解决方案:正确地将参数强制转换为整数。如果在方法的第一行中,我使用$offset=(int)$offset代码>它可以工作。之前我尝试在$query->bindParam()方法中执行强制转换
路线看起来像:
$app->get("/members/next10/:offset", function($offset) use($app) {
$offset = (int) $offset;
$app->response()->header('Content-Type', 'application/json');
try {
$pdo = connectToDatabase();
$query = $pdo->prepare("SELECT * FROM `members` LIMIT 10 OFFSET :offset");
$query->bindParam(":offset", $offset, PDO::PARAM_INT);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
if (!empty($result)) {
echo json_encode($result);
} else {
$app->response()->status(404);
}
} catch (PDOException $d) {
$app->response()->status(500);
var_dump($d->getMessage());
}
});
可能重复