当我尝试执行以下查询时,我收到以下警告。
错误
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters in E:\xampp\htdocs\msdca\attendance.php on line 142
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in E:\xampp\htdocs\msdca\attendance.php on line 142
询问
$name = $_GET['name'];
$stmt = $pdo->prepare("SELECT `st_id`, `st_name` FROM `students` WHERE st_name LIKE ? or st_phone LIKE ? AND atd_year = :year");
$param = array("%$name%", "%$name%");
$stmt-> bindValue(':year', $_GET['yid']);
$stmt-> execute($param);
可能是什么原因引起了这个问题?任何帮助将受到高度赞赏。谢谢。
您的问题正是错误消息所说的,您在同一查询中使用位置参数(?
)和命名参数(:year
),这是不允许的。您需要始终如一地使用其中一个,例如
$name = $_GET['name'];
$stmt = $pdo->prepare("SELECT `st_id`, `st_name` FROM `students` WHERE st_name LIKE ? or st_phone LIKE ? AND atd_year = ?");
$param = array("%$name%", "%$name%", $_GET['yid']);
$stmt-> execute($param);
或使用命名参数:
$name = $_GET['name'];
$stmt = $pdo->prepare("SELECT `st_id`, `st_name` FROM `students` WHERE st_name LIKE :name or st_phone LIKE :phone AND atd_year = :year");
$param = array(':name' => "%$name%", ':phone' => "%$name%", ':year' => $_GET['yid']);
$stmt-> execute($param);