php - fatal error : Call to a member function free() on boolean

标签 php mysql mysqli

<分区>

我的服务器上的数据库出现错误。在我的本地计算机上一切正常。我已通过我的主机确认数据在服务器上并且查询有效,但我不断收到此错误消息:

Fatal error: Call to a member function free() on boolean

我的 PHP 代码在同一数据库中的另一个表上成功运行。我已经在 phpMyAdmin 上测试了查询,它可以处理数据。但是当我测试 $result 时,它仍然是错误的。我不明白为什么。这是我的 PHP 代码:

<?php

    // Connect to the database by creating a new mysqli object
    include "inc/DBconnect.php";

    // Run a query and put the result into a new variable named $result -- It's just a table of results
    $result = $mysql->query("
SELECT
    staffId,
    company,
    fName,
    lName,
    title,
    contact1,
    contact2,
    department,
    comments,
    lastEdit
FROM staff WHERE archive = 0 ORDER BY lName");

    // Loop through the result from 0 to the number of rows in the result, one row at a time. $i will contain
    // the current row number every pass through the loop ( you could do it backwards too but why? )
    for ($i = 0; $i < $result->num_rows; $i++) {
    // Move to row number $i in the result set.
    $result->data_seek($i);

    // Get all the columns for the current row as an associative array -- we named it $aRow
    $aRow = $result->fetch_assoc();

    $staffId = $aRow['staffId'];

        // Write a table row to the output containing information from the current database row.
        print "<p>";
        print "<staffName><a href='staffDetails.php?ID=$staffId'>" . $aRow['fName'] . " " . $aRow['lName'] . "</a>&nbsp;&nbsp;(" . $aRow['staffId'] .  ")</staffName>";
        print "<title>" . $aRow['title'] . "</title>";
        print "<contact>" . $aRow['contact1'] . " | " . $aRow['contact2'] . "</contact>";
        print "<company>" . $aRow['company'] . "</company>";
        print "<department>" . $aRow['department'] . "</department>";
        print "</p>";
    }

    // Very important: Close your result set to free up the memory it's taking.
    $result->free()
    ?>

最佳答案

您应该改用 while 循环:

if ($result = $mysql->query($query)) {
    while ($aRow = $result->fetch_assoc()) {
        $staffId = $aRow['staffId'];
        print "<p>";
        print "<staffName><a href='staffDetails.php?ID=$staffId'>" . $aRow['fName'] . " " . $aRow['lName'] . "</a>&nbsp;&nbsp;(" . $aRow['staffId'] .  ")</staffName>";
        print "<title>" . $aRow['title'] . "</title>";
        print "<contact>" . $aRow['contact1'] . " | " . $aRow['contact2'] . "</contact>";
        print "<company>" . $aRow['company'] . "</company>";
        print "<department>" . $aRow['department'] . "</department>";
        print "</p>";
    }
}else{
   printf("Errormessage: %s\n", $mysql->error);
}

然后删除对 data_seek()free() 的调用

编辑:

$mysql = new mysqli("xxxxx", "xxx", "xxx", "xxx");

if ($mysql->connect_errno) {
    printf("Connect failed: %s\n", $mysql->connect_error);
    exit();
}

$query = "...";

if ($result = $mysql->query($query)) {
    while ($aRow = $result->fetch_assoc()) {
        $staffId = $aRow['staffId'];
        print "<p>";
        print "<staffName><a href='staffDetails.php?ID=$staffId'>" . $aRow['fName'] . " " . $aRow['lName'] . "</a>&nbsp;&nbsp;(" . $aRow['staffId'] .  ")</staffName>";
        print "<title>" . $aRow['title'] . "</title>";
        print "<contact>" . $aRow['contact1'] . " | " . $aRow['contact2'] . "</contact>";
        print "<company>" . $aRow['company'] . "</company>";
        print "<department>" . $aRow['department'] . "</department>";
        print "</p>";
    }
}else{
   printf("Errormessage: %s\n", $mysql->error);
}

$mysqli->close();

关于php - fatal error : Call to a member function free() on boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34496010/

相关文章:

mysql - 获取一个表中的所有记录以及第二个表中的相对最后一条记录

mysql - 具有int参数的函数并返回varchar会导致SQL语法错误#1064

php - 限制偏移量大于实际记录的mysql select语句

php - Mysqli PHP我希望未知服务器主机时出现自定义错误消息

php - MySQL 值作为 PHP session

php - Magento 免费送货和折扣券

php - 未捕获的异常 'PDOException',消息为 'could not find driver'

php - 错误 : There is no `_sonata_admin` defined for the controller

php - 两个 while 循环从两个查询中获取数据只显示一个结果

php - 在连接表中计算结果并在侧边栏中显示 () 之间