【问题标题】:How can I use mysqli_stmt_bind_param for integers?如何将 mysqli_stmt_bind_param 用于整数?
【发布时间】:2013-12-13 16:43:00
【问题描述】:

据我所知,mysqli_real_escape_string 不如准备好的语句安全,所以我要进行转换。
但是,我在最简单的代码中遇到了一个小问题。
我期望执行的查询是SELECT * from forums WHERE id=1,并针对数据库行进行了测试。

这是我正在使用的准备好的语句代码。

$con=mysqli_connect('a','b','c','d');
$forum = $_GET["forum"];
$stmt = mysqli_prepare($con, "SELECT * from forums WHERE id=?");
echo mysqli_stmt_bind_param($stmt, "i", $forum);
mysqli_stmt_execute($stmt);
mysqli_stmt_fetch($stmt);
mysqli_stmt_store_result($stmt);
if (mysqli_stmt_num_rows($stmt)<1){
    echo "0";
    }

这似乎返回 10,这意味着查询绑定成功,但没有找到任何行。

如果这看起来微不足道,我很抱歉,但这对我来说没有多大意义。

【问题讨论】:

  • 什么是$_GET['forum']?你确定有结果吗?
  • @ExplosionPills 这个代码在我使用普通的 mysqli 扩展时有效
  • 为什么您认为您的问题与 mysqli_stmt_bind_param 相关?
  • 因为 mysqli_query 在这段代码中运行良好
  • 您将准备好的语句视为结果。我不认为这有任何希望以目前的形式工作。这在很大程度上源于使用老派的程序风格,很容易犯这类错误。

标签: php mysql mysqli


【解决方案1】:

我想你还需要mysqli_stmt_bind_resultmysqli_stmt_store_resultmysqli_stmt_fetch 才能得到一些结果……至少手册上是这么说的。

【讨论】:

    【解决方案2】:

    除非您使用mysqli_stmt_store_result,否则mysqli_stmt_num_rows 将返回0,因为mysqli 尚未获取数据并且不知道有多少行。

    mysqli_stmt_store_result 将来自 MySQL 的所有结果数据缓存在内存中以供 PHP 后续使用,但它可能会导致数据集大或内存限制低的内存不足错误。

    【讨论】:

    • mysqli_stmt_store_result 不存在。最接近的匹配:
    • 我已经编辑了添加这些功能的问题,但没有任何反应
    猜你喜欢
    • 2023-03-11
    • 2017-06-17
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2012-02-21
    • 1970-01-01
    相关资源
    最近更新 更多