类型定义字符串与绑定数不匹配

Type definition string doesn't match number of bind

我在 php 中有一个错误,我不知道如何解决这个问题。顺便说一句,这是我的学校课程示例,所以我真的不知道那里发生了什么。这被支持为 master/detail 导航。

<?php

$mysqli = new mysqli("localhost", "root", "", "base");

$stmt = $mysqli->prepare('SELECT * FROM aeromiting WHERE id = ":id"');
echo $mysqli->error;
$stmt->bind_param(':id', $_GET['id']);
var_dump($stmt);

$data = $stmt->execute();

?>

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in C:\xampp\htdocs\test1\detail.php on line 20 object(mysqli_stmt)#2 (10) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(0) ["field_count"]=> int(4) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }

您正在此处混合 API。 MySQLi 不能接受字符串或类似的参数。

$stmt = $mysqli->prepare('SELECT * FROM aeromiting WHERE id = ":id"');
$stmt->bind_param(':id', $_GET['id']);

应该是

$stmt = $mysqli->prepare('SELECT * FROM aeromiting WHERE id = ?');
$stmt->bind_param('i', $_GET['id']);

这当然假设$_GET['id']是一个整数(因此bind_param中的'i'。如果是字符串,将i替换为s.

您可能还应该得到一个 bind_result,因此您实际上将数据库的结果绑定到一些变量。看看 MySQLi documentation.

如果你想在 PDO 中做,试试这个...

<?php
$host = 'localhost'; $db = 'base'; $user = 'root'; $pw = '';
$conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user,     $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {
    $id = $_GET['id'];
    $sql = "SELECT * FROM aeromiting WHERE id=:id";
    $query = $conn->prepare($sql);
    $query->bindValue(':id', $id, PDO::PARAM_INT);
    $query->execute();
    $row = $query->fetch(PDO::FETCH_ASSOC);
    $totalRows = $query->rowCount();
} catch (PDOException $e) {
die("Could not get the data: " . $e->getMessage());
}
?>

查看 pdo_mysql 了解更多信息。