php - 从 PDO 获取“无效参数号:未绑定参数”
问题描述
我的代码报告了这个错误:
参数号无效:第 6 行没有绑定参数,即 statement->execute();
代码
<?php
require 'db.php';
$id = $_GET['id'];
$sql = 'SELECT * FROM post where id=:id';
$statement = $connection->prepare($sql);
$statement->execute();
$posts = $statement->fetch(PDO::FETCH_OBJ);
?>
<?php require 'header.php'; ?>
<div class="container">
<div class="card mt-3">
<div class="card-header">Comment on Post <?= $posts->post ?> </div>
</div>
</div>
<?php require 'footer.php'; ?>
我只想在 id 所在的帖子中获取数据
解决方案
至少你正在使用准备好的语句,但是在你的准备中你有id=:id
,所以你需要将一个值传递给执行以代替:id
...
$statement->execute(['id' => $id]);
推荐阅读
- reactjs - 在 React-admin 列表视图中使用输入
- jsp - 找到一个带有 GET 参数的 .jsp 网站的 URL,其中输入的编码可能类似于 base64
- laravel - 未找到类别
- c# - iis 上的 web api 主机接收 zip 问题
- sql - 雪花为每条记录添加具有唯一随机值的新列
- javascript - 使用 JQuery 定位多个类时,必须使用“each()”吗?最佳实践
- python - 如何拆分 CSV 文件中的值
- image - Flutter - 图像到字符串base64?
- arrays - 按姓氏对字符串进行排序:Ruby
- audiokit - 带有 AKMIDISampler (sf2) 的 AKAppleSequencer 在 iO14 中一次只播放一个音符