致命错误:在布尔值上调用成员函数free()

时间:2015-12-28 15:12:39

标签: php mysql

我的服务器上的数据库出错了。在我的本地电脑上一切都很好。我已通过我的主机确认数据在服务器上并且查询有效,但我不断收到此错误消息:

  

致命错误:在布尔值

上调用成员函数free()

我的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()
    ?>

任何帮助将不胜感激。 谢谢!

1 个答案:

答案 0 :(得分:1)

您应该使用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();