0

I know this type of question has been asked a million times before, but I haven't been able to find the right answer for me. My situation is as follows:

I click a button and after that I call both a mysqli SELECT and INSERT function from my database class. The SELECT function is executed perfectly, but right after that I get the fatal error you can see in the title.

This is the line I'm trying to execute:

insertPrefixes($dbAddress, $netmask, $prefixName, $nrOfHosts, getParentID($longest));

The function itself:

function insertPrefixes($dbAddress, $netmask, $prefixName, $nrOfHosts, $prefixParent = 0) {
    $db = new db();

    /* Prepare data to be inserted */
    $table = "prefix";

    if($prefixParent < 1) {
        $data = array("pfx_prefix" => $dbAddress,
            "pfx_length" => $netmask,
            "pfx_version" => 4,
            "pfx_name" => $prefixName,
            "pfx_capacity" => $nrOfHosts);
        $typeDef = array("i", "i", "i", "s", "i");
    } else {
        $data = array("pfx_prefix" => $dbAddress,
            "pfx_length" => $netmask,
            "pfx_version" => 4,
            "pfx_name" => $prefixName,
            "pfx_capacity" => $nrOfHosts,
            "" => $prefixParent);
        $typeDef = array("i", "i", "i", "s", "i", "i");
    }

    /* Insert the data into the database */
    $db->sql_insert($data, $typeDef, $table);

    $db->disconnect();
}

The line where it goes wrong (in database.class.php) in the 'sql_insert' function:

/* Prepare statement */
$stmt = $this->connect()->stmt_init();
if ($stmt->prepare($sql)) {
    // statement code
}

Both my SELECT and INSERT functions work perfectly on their own, but once I call both the function in the same run, I always get the 'Call to a member function stmt_init() on a non-object' error. Is there something I am forgetting? If you need more code please ask :)

Beeelze
  • 503
  • 1
  • 9
  • 25
  • It means that `$this->connect()` does not return an object. We do not know why it doesn't, because we can't see it. Apparently it does something that causes it to fail when called *a second time...*!? – deceze Jan 20 '15 at 08:48
  • Yeah, I figured the same thing. I tried closing the statement after execution and closing the database connection after each function as you can see, but none of these things help. I will check out the Reference you linked me. Thanks! – Beeelze Jan 20 '15 at 08:55
  • 1
    Debug your code. `var_dump($this->connect())`. What *does* it return? Trace back in your code to figure out *why* it returns that. – deceze Jan 20 '15 at 08:58
  • Right now I am creating multiple object instances when I do $this->connect()->stmt_init(). I think I need to do this once, prepare my statements and then execute them. – Beeelze Jan 20 '15 at 10:45

0 Answers0