0

I had a custom detailed form and calculator built for a Wordpress site a couple years ago. We just migrated the site to a new host and I got an issue with the code. Since it was custom and coded a specific way, I got an error and had to change the database, user and password for MySQL from the old to the new. Just after I did that, I am getting a an error

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

I have not changed anything in the code at all. Only after migration did I get the errors but I changed the db info only, I am unsure why I am getting a new error?

<?php
    $host='localhost';
    $port=3306;
    $socket='';
    $user='my_user';
    $password='passW0rd!';
    $dbname='my_db';

    $con = new mysqli($host, $user, $password, $dbname, $port, $socket) or die ('Could not connect to the database server' . mysqli_connect_error());

    //echo "Connected to Database<br />";
    $sql = "SELECT ID, Description FROM adv_sources WHERE WebItem = '-1' ORDER BY Description ASC";
    $result = $con->query($sql);

    $options = "";
    echo "<label for 'adv_source'>How did you hear about Us?</label>\n";
    echo "<select name='adv_source' id='adv_source' class='pure-u-22-24' required>\n";
    echo "<option value=''>[Choose One Please]</option>\n";

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {

            $id = $row["ID"];
            $thing = $row["Description"];
            //$options.="<option value=\"$id\">".$thing."</option>"; 
            echo "<option value=\"$id\">" . $thing . "</option>\n";
        }
    }
    echo "</select>";
    $result->free();
    $con->close();
?>

I believe the error is with $result, but I know next to nothing about PHP. I get the problem with the function and code not returning a boolean but I have tried for a couple hours to research this and still nothing with the project due date in 8 hours (and I would like to get some sleep).

Derk Jan Speelman
  • 11,291
  • 4
  • 29
  • 45
  • where is the call to `myfunction` in the above code? – Professor Abronsius May 08 '19 at 06:40
  • Your query is failing. Change `$result = $con->query($sql);` to `$result = $con->query($sql) or die($con->error);` to find out why. – Nick May 08 '19 at 06:40
  • Thanks Nick, I guess I am missing a table. I do not think I have enough knowledge to build the necessary table, I've never even used mysqli, I am going to try to hire a freelancer if I can stay up late enough. Appreciate the help! – user1008028 May 08 '19 at 06:54
  • @user1008028 glad you figured out the problem. Perhaps that table can be restored from the old host? – Nick May 08 '19 at 13:09

1 Answers1

0

When the query fails it will return false. You should put all relevant code after $result = $con->query($sql); in a if ($result !== false) { check.

Daan
  • 191
  • 10