【问题标题】:Echoing result of using $_GET to query MySQL w/ PHP使用 $_GET 使用 PHP 查询 MySQL 的回显结果
【发布时间】:2016-10-17 21:29:04
【问题描述】:

如何回显这段代码的结果?

编辑:这是新代码。基于官方 PHP 网站的示例。

<?php
require 'connection.php';
$getid = $_GET["id"];
$sql = "SELECT * FROM pasteinfo WHERE id = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param("i", $getid);


if ($stmt = $mysqli->prepare($sql)) {
    $stmt->execute();
    $stmt->bind_result($paste)
        while($stmt->fetch()) {
            printf ("%s \n", $paste);
        }

    $stmt->close();
    $mysqli->close();

}
?>

【问题讨论】:

  • @MarcB 所以我的新代码有 while ($stmt->fetch()) { printf(%s); } 就在 $stmt->execute();但它使页面直接不可用?
  • 或者您实际上可以阅读链接文档中的示例,因此您实际上可以正确使用它。另外,printf(%s) 是一个明显的语法错误。
  • @MarcB pastebin.com/C7ydHJuG 我的错!新代码在那里,有同样的问题。
  • 将代码放在问题中,而不是远程链接。

标签: php mysql mysqli get


【解决方案1】:

在您调用bind_param() 之后,您再次准备了语句,因此新语句没有参数绑定。在调用bind_result() 之后,您还缺少;

<?php
require 'connection.php';
$getid = $_GET["id"];
$sql = "SELECT * FROM pasteinfo WHERE id = ?";

if ($stmt = $mysqli->prepare($sql)) {
    $stmt->bind_param("i", $getid);
    $stmt->execute();
    $stmt->bind_result($paste);
    while($stmt->fetch()) {
        printf ("%s \n", $paste);
    }

    $stmt->close();
    $mysqli->close();

}
?>

bind_result 中的变量数也应与您选择的列数相同。少一些可能没问题,它只会忽略剩余的列,我不确定。最好选择特定的列而不是select *,因为根据表定义中的列顺序是危险的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    相关资源
    最近更新 更多