Php 警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:未绑定任何参数6

Php 警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:未绑定任何参数6,php,pdo,Php,Pdo,如果需要使用bindParam()方法: <?php .... require 'connect.php'; $sql = "SELECT * FROM subject WHERE subId=:id"; $getrows = $conn->prepare($sql); $getrows->execute(); $rows = $getrows->fetchAll(); foreach($rows as $row){ ?&

如果需要使用
bindParam()
方法:

<?php

....

require 'connect.php';

    $sql = "SELECT * FROM subject WHERE subId=:id";
    $getrows = $conn->prepare($sql);
    $getrows->execute();
    $rows = $getrows->fetchAll();
    foreach($rows as $row){


?>

您需要在execute方法中绑定param,如下所示:

$id = 1;
$sql = "SELECT * FROM subject WHERE subId=:subId";
$sql = bindParam(':subId', $id, PDO::PARAM_INT)
$getrows = $conn->prepare($sql);
$getrows->execute();
$rows = $getrows->fetchAll();
foreach($rows as $row){
    print_r($row);
}
如下所示:您应该在查询中绑定参数“id”,使用PDO或:从subId=“.id”所在的subject中选择*。您在查询中有占位符(
:id
),但从不向其传递任何值。只需执行:
$getrows->execute([':id'=>$id]);
(用实际变量/值替换
$id
)。
$getrows->execute([':subId' => $myid])