这是我的代码
$sql3= "select *
from comments
where status=:status
limit=:limit
offset=:offset
order by time desc";
$query3= $pdo->prepare($sql3);
$query3->bindValue(":status",'n');
$query3->bindValue(":limit",$per_page);
$query3->bindValue(":offest",$offset);
$query3->execute();
$comments=$query3->fetchall();
这里的注释是我的表名状态,时间是我表中的两列。每当我运行此代码时,它都会显示警告
警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:参数未在E:\ XAMPP \ htdocs \ parlament \ user \ logged_in_area.php中定义
这是什么意思?
答案 0 :(得分:2)
答案取决于limit
和offset
。
AND
/ OR
运算符$sql3= "select *
from comments
where status=:status
and `limit`=:limit
and `offset`=:offset
order by time desc";
$query3= $pdo->prepare($sql3);
$query3->bindValue(":status", 'n');
$query3->bindValue(":limit", $per_page);
$query3->bindValue(":offest", $offset);
$query3->execute();
$comments=$query3->fetchall();
LIMIT <n>
,而非LIMIT = <n>
(OFFSET
相同)PDO::PARAM_INT
指定类型(OFFSET
相同)ORDER BY
和LIMIT
之前添加OFFSET
$sql3= "select *
from comments
where status=:status
order by time desc
limit :limit
offset :offset";
$query3= $pdo->prepare($sql3);
$query3->bindValue(":status", 'n');
$query3->bindValue(":limit", (int)$per_page, PDO::PARAM_INT);
$query3->bindValue(":offset", (int)$offset, PDO::PARAM_INT);
$query3->execute();
$comments=$query3->fetchall();
答案 1 :(得分:1)
添加我的答案,因为还没有人提到这个特定的部分......
MySQL对LIMIT
参数的数据类型非常挑剔。您几乎需要使用bindParam(':limit', $per_page, PDO::PARAM_INT)
。我对OFFSET
假设相同。
所以,总结
// because E_WARNING level errors are insufficient
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT * FROM `comments` WHERE `status` = :status ORDER BY `time` DESC LIMIT :limit OFFSET :offset');
$stmt->bindValue(':status', 'n');
$stmt->bindParam(':limit', $per_page, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT); // spelt "offset"
$stmt->execute();
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
答案 2 :(得分:0)
试试这个 - &gt;
$sql3= "select *
from comments
where status = ?
limit ?
offset ?
order by time desc";
$query3= $pdo->prepare($sql3);
$query3->execute(array('n',$per_page,$offset));
$comments=$query3->fetchall();
答案 3 :(得分:-1)
不确定,但我认为PDO :: bindValue / bindParam适用于变量引用。您不能将静态值设置为参数。 尝试替换
$query3->bindValue(":status",'n');
通过
$n_value = 'n';
$query3->bindValue(":status",$n_value);
你也忘记了&#34; AND&#34;您的条件之间的关键字
答案 4 :(得分:-2)
您在语句中缺少AND
或OR
关键字。此外,LIMIT
是reserved keyword,如果您不想这样做,则需要对其进行反引号或将其重命名为其他内容。
答案 5 :(得分:-4)
$sql3= "select *
from comments
where status = ?
limit= ?
offset= ?
order by time desc";
$query3= $pdo->prepare($sql3);
$query3->bindValue(1,'n');
$query3->bindValue(2,$per_page);
$query3->bindValue(3,$offset);
$query3->execute();
$comments=$query3->fetchall();