2

I am getting an error with a database on my server. On my local computer everything is fine. I have confirmed through my host that the data is on the server and that the query works but I keep getting this error message:

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

My php code is successfully working on another table in the same database. I have tested the query on phpMyAdmin and it works with the data. But when I test $result it still comes up false. I don't understand why. Here is my php code:

<?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()
    ?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Abigail Hardin
  • 167
  • 2
  • 13
  • The post that you're telling me to look at says that the query is failing. But I have tried the query directly on phpMyAdmin and it works. – Abigail Hardin Dec 28 '15 at 16:08

1 Answers1

1

You should use a while loop instead:

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);
}

then remove the call to data_seek() and free()

EDIT:

$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();
meda
  • 45,103
  • 14
  • 92
  • 122
  • This removes the error. But no data is printing. I have tested the query on phpMyAdmin and it works, but $result is still False. – Abigail Hardin Dec 28 '15 at 16:33
  • @AbigailHardin you need to print the error if it returns false, see my edit – meda Dec 28 '15 at 16:52
  • Thanks. I'm getting no results but it's just printing Errormessage: . There is no mysql error. – Abigail Hardin Dec 28 '15 at 16:59
  • @AbigailHardin is your connection successful? nothing would work if it is not see my edit – meda Dec 28 '15 at 17:03
  • I'm not getting a connection error. And I've successfully connected to a different table in the same database. I have no idea why it is not working. – Abigail Hardin Dec 28 '15 at 17:04
  • Adding `$mysqli->close();` gives me Fatal Error: Call to a member function close() on null – Abigail Hardin Dec 28 '15 at 17:11
  • @AbigailHardin you need to get the errors to show `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top of your script – meda Dec 28 '15 at 17:11
  • 1
    You can also add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` right before the connection – meda Dec 28 '15 at 17:19
  • 1
    Thanks! This not only fixed it, it also showed me some new ways that I can check for future errors. Thank you! – Abigail Hardin Dec 29 '15 at 14:10