4

I get an error with this text:(sorry for my bad english I am from germany!)

Error:Fatal error: Call to a member function bind_param() on a non-object in /users/ftf/www/ccache.php on line 44

A part of the Code from ccache.php

     // Neues Datenbank-Objekt erzeugen
    $db = @new mysqli( 'localhost', 'ftf', '***', 'ftf' );
    // Pruefen ob die Datenbankverbindung hergestellt werden konnte
    if (mysqli_connect_errno() == 0)
    {
        $sql = "INSERT INTO cache
('name', 'user', 'veroefentlichung', 'beschreibung', 'FTFcode', 'STFcode', 'TTFcode', 'type', 'lat', 'lon', 'address', 'link')
VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')";
$eintrag = $db->stmt_init();
$eintrag = $db->prepare( $sql );

        $eintrag->bind_param($titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44

        $eintrag->execute();
        // Pruefen ob der Eintrag efolgreich war
        if ($eintrag->affected_rows == 1)
        {
            echo 'Der neue Eintrage wurde hinzugefügt.';
        }
        else
        {
            echo 'Der Eintrag konnte nicht hinzugefügt werden.';
        }
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jonas Franz
  • 555
  • 1
  • 8
  • 18
  • http://stackoverflow.com/search?q=Fatal+error%3A+Call+to+a+member+function+bind_param%28%29+on+a+non-object – Mike B Sep 20 '13 at 16:41

4 Answers4

4

Check your return values!

Bad: $eintrag = $db->prepare( $sql )

Good:

if( ! $eintrag = $db->prepare( $sql ) ) {
  echo 'Error: ' . $db->error;
  return false; // throw exception, die(), exit, whatever...
} else {
  // the rest of your code
}

The same goes for $eintrag->execute();.

Also, the problem is probably the fact that you're wrapping your ? placeholders in quotes. Don't do that. MySQLi does that for you.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • Nothing happens. The error is on Line 44: `$eintrag->bind_param($titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44 ` – Jonas Franz Sep 20 '13 at 16:54
  • Then it's your `prepare()` that is failing. `if( ! $eintrag = $db->prepare( $sql ) ) {` and the rest is the same. – Sammitch Sep 20 '13 at 16:56
  • @user2792899 Update answer's code, added the probable cause of your error. – Sammitch Sep 20 '13 at 16:59
3
  $eintrag->bind_param($titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon,       $shortdesc, $genlink); // line 44

You need to the define the type of parameters as this:

$eintrag->bind_param("ssssssiiss", $titel, $user, $datum, $desc, $FTF, $STF, $TTF, $type, $Lat, $Lon, $shortdesc, $genlink); // line 44

s - string i - int Also check documentation: http://php.net/manual/en/mysqli-stmt.bind-param.php

2

The error message Call to a member function bind_param() on a non-object... means that you haven't properly instantiated the object $eintrag before calling bind_params() on it.

It could be because you are trying to instantiate $eintrag from $db, and it is your line $db = @new mysqli( 'localhost', 'ftf', '***', 'ftf' ); which is actually failing.

Try removing the '@', then you can at least read any errors/notices/warnings:

$db = new mysqli( 'localhost', 'ftf', '***', 'ftf' );

Jonas Franz
  • 555
  • 1
  • 8
  • 18
bdf
  • 247
  • 1
  • 8
  • There still is: `Fatal error: Call to a member function bind_param () on a non-object in / users / ftf / www / ccache.php on line 44` – Jonas Franz Sep 20 '13 at 17:08
  • Could be your line `$eintrag = $db->stmt_init();`...i am not sure you need that line. Check example #1 at http://www.php.net/manual/en/mysqli-stmt.bind-param.php – bdf Sep 20 '13 at 17:16
1

Change the code to:

$sql = "INSERT INTO cache
        (name, user, veroefentlichung, beschreibung, FTFcode, STFcode, TTFcode, type, lat, lon, 'address', 'link')
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

(i.e. delete the quotes)