【问题标题】:I am facing problem while fetching data from database using prepared statement我在使用准备好的语句从数据库中获取数据时遇到问题
【发布时间】:2019-01-17 12:10:23
【问题描述】:

我的数据库名称是“购物车”。 表名是“产品”。 行是'id'、'name'、'image'、'price'。

我收到此错误。

警告:mysqli_stmt_bind_param():变量数与第 9 行 E:\xammp\htdocs\cart\cart.php 中准备好的语句中的参数数不匹配强>

Here is the screenshot

这是代码

<?php
$connect = mysqli_connect('localhost', 'root', '', 'cart');
$query = "SELECT * FROM `products`";
$stmt = mysqli_prepare($connect, $query);

if($stmt){
    mysqli_stmt_bind_param($stmt, 'issi', $id, $name, $image, $price);
    mysqli_stmt_bind_result($stmt, $id, $name, $image, $price);
    mysqli_stmt_execute($stmt);
    while(mysqli_stmt_fetch($stmt)){
        echo "<pre>";
        echo $id;
        echo "</pre>";
    }
}

谁能帮忙?

【问题讨论】:

  • 绑定参数个数不匹配。
  • 我知道,但我无法理解这一点。您能指出来吗??绑定参数是4。第一个是int,第二个和第三个是string,最后一个也是int。

标签: php mysql


【解决方案1】:

首先,请不要使用“Select *”,而是选择您的列。

取消注释以下行:

 mysqli_stmt_bind_param($stmt, 'issi', $id, $name, $image, $price);

你不需要这个来绑定你的结果中的参数。

如果您的 SELECT 语句中有任何要绑定的参数,您可以使用 mysqli_stmt_bind_param(....),但这里不可能,所以会出现此错误。

更多信息在这里:http://php.net/manual/de/mysqli-stmt.bind-result.php

【讨论】:

    【解决方案2】:

    如我所见,您没有在查询中使用任何绑定参数,例如“SELECT * FROM product WHERE price = ? AND id = ?”

    http://php.net/manual/de/mysqli.prepare.php

    【讨论】:

    • 我不想选择任何特定数据。我想从表中选择所有数据。
    • 我认为,您不需要绑定参数,而只需从查询“SELECT * from table_name”中获取结果
    • 那么,我应该删除这个链接“mysqli_stmt_bind_param($stmt, 'issi', $id, $name, $image, $price);"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    相关资源
    最近更新 更多