首页 > 解决方案 > 从 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 所在的帖子中获取数据

在此处输入图像描述

标签: phppdo

解决方案


至少你正在使用准备好的语句,但是在你的准备中你有id=:id,所以你需要将一个值传递给执行以代替:id...

$statement->execute(['id' => $id]);

推荐阅读