【问题标题】:PHP and Mysqli code error messagePHP 和 Mysqli 代码错误信息
【发布时间】:2013-07-30 16:21:43
【问题描述】:

我有这个 PHP 代码,网站上的另一个用户对我很有帮助。它试图将数据库中的条目与用户在表单中提供的内容进行匹配,并回显出符合这些规范的图片。

(我在前 4 行定义了我的数据库信息,但本网站不想写出来)

// Errors
$error = array();
if (!isset($_POST['submit']))
    $error[] = "The form was not set.<br />";

// Here we check if each of the variable are set and have content
if (!isset($_POST['gender']) || strlen($_POST['gender']) == 0)
    $error[] = "You must fill the gender field.<br />";

if (!isset($_POST['hair']) || strlen($_POST['hair']) == 0)
    $error[] = "You must fill the hair field.<br />";

if (!isset($_POST['height']) || strlen($_POST['height']) == 0)
    $error[] = "You must fill the height field.<br />";

if (!isset($_POST['body']) || strlen($_POST['body']) == 0)
    $error[] = "You must fill the body field.<br />";

if (empty($error))
{
    $con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
    if ($con->connect_error)
        die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

    // Here we prepare your query and make sure it is alright
    $sql = "SELECT * FROM `table` WHERE gender=? AND hair=? AND height=? AND body=?";
    if (!$stmt = $con->prepare($sql))
        die('Query failed: (' . $con->errno . ') ' . $con->error);

    // Here we define the field types with 'ssss' which means string, string, string
    // and also set the fields values
    if (!$stmt->bind_param('ssss', $_POST['gender'], $_POST['hair'], $_POST['height'], $_POST['body']))
        die('Binding parameters failed: (' . $stmt->errno . ') ' . $stmt->error);

    // Now we finally execute the data to update it to the database
    // and if it fails we will know
    if (!$stmt->execute())
        die('Execute failed: (' . $stmt->errno . ') ' . $stmt->error);

    // Now we read the data
    while ($row = $stmt->fetch_object())
    {
        $pic = $row->picture;
        echo $row->picture, "\n";
    }
    $stmt->close();
    $con->close();
}
else
{
    echo "Following error occurred:<br />";
    foreach ($error as $value)
    {
        echo $value;
    }
}
?>

我在第 48 行收到此错误消息

Fatal error: Call to undefined method mysqli_stmt::fetch_object() in /home/content/...

就是这行代码:

// Now we read the data
while ($row = $stmt->fetch_object())

【问题讨论】:

  • MySQLi 准备好的语句不能像 query() 那样调用 fetch_object()。您必须将bind_result() 放入变量中,然后调用fetch() 来填充这些变量。这可能是 PDO 通常更受欢迎的主要原因。使用 MySQLi 准备好的语句,获取是一个很大的麻烦。
  • 查看MySQLi prepare() docs 中的示例以了解如何处理提取。

标签: php mysqli


【解决方案1】:

您不能直接从mysqli_stmt 对象中获取结果。首先使用get_result 获取mysqli_result 对象,然后在其上使用fetch_object

$result = $stmt->get_result();
while ($row = $result->fetch_object())

请注意,这仅在您安装了 mysqlnd 时才有效;如果没有,请参阅 bind_resultfetch 方法,了解不太方便(但更兼容)的方法。

【讨论】:

  • 请注意 get_result() is not available on all systems 因为它依赖于较新的 mysqlnd 驱动程序。
  • 感谢 Robadob 的编辑通知我有一个 fetch_object 方法;不幸的是,我的编辑破坏了他的编辑。
猜你喜欢
  • 2011-02-11
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多