php - bind_param 错误,其中提供了正确数量的参数?

标签 php mysql sql

下面非常简单的脚本旨在根据 GET 变量返回名字和姓氏。

每次运行脚本时都会显示此错误:

警告:mysqli_stmt::bind_param():变量数量与第 10 行 mypath/names.php 中准备好的语句中的参数数量不匹配

我已经检查了 SQL 查询,它在 SQLyog 中运行得非常好。我还使用 get 变量调用该文件:names.php?surname=jo

<?php
ini_set("display_errors",1);
require_once('connection.php');

$db = DB::connect();
$db->select_db("mydb");
$sql = "SELECT forename,surname FROM `people` WHERE `surname` LIKE '?%' LIMIT 1";

if ($stmt = $db->prepare($sql)) {
    $stmt->bind_param("s", $_GET['surname']);
    $stmt->execute();
    $stmt->bind_result($forename, $surname);
    $stmt->fetch();
    $stmt->close();
}
?>

最佳答案

你应该尝试:

$sql = "SELECT forename,surname FROM `people` WHERE `surname` LIKE ? LIMIT 1";

$surname=$_GET['surname']."%";
$stmt->bind_param("s", $surname);

关于php - bind_param 错误,其中提供了正确数量的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20563250/

相关文章:

java - 我应该如何指定托管 bean 方法来保存定向外键上的数据?

MySQLIMPORT 十六进制值

mysql - 维度建模 : how to create a table without Surrogate Primary Keys?

sql - 从表格中选择 TOP x

php - 我需要显示位于另一台服务器上的另一个数据库的值

PHP MYSQL $_GET ['ID' ] 来自动态生成页面上的表

php - 循环内循环达到内存限制

php - 检查数据库中的值并递增字母

php - Ubuntu 上本地的 Nginx phpmyadmin 和 magento 2

sql - oracle加入一对多关系,返回第一个加入日期