0

I'm getting this error and have no clues why:

Fatal error: Call to a member function bind_param() on a non-object

$string = file_get_contents($url, false, $context);
$xml    = new SimpleXMLElement($string);

if ($stmt->prepare("INSERT INTO table (id, filePointer, amount, description) 
                        VALUES (?, ?, ?, ?)"))
{
    foreach ($xml->machine as $machine) 
    {
        $stmt->bind_param('ssss', $machine->id, $machine->images->image->filePointer, $machine->advertised_price->amount, $machine->description);
        // Execute the query
        $stmt->execute();
        $stmt->close();
    }

}

Here is how I'm calling the table up:

//Create table
$create_table =
'CREATE TABLE IF NOT EXISTS table  
(
    id INT NOT NULL,
    filePointer TEXT NOT NULL,
    amount INT NOT NULL,
    description TEXT NOT NULL
    PRIMARY KEY(id)
)';

$create_tbl = $db->query($create_table);
if ($create_table)
{
    echo "Table has been created";
} 
else 
{
        echo "error!!";  
}

Previously I was making my table in MyPHPAdmin. When I run this and then check my database, it is not creating a table OR updating it. I have checked to make sure it is connecting to the database and as far as I can tell it is working.

My new INSERT INTO code:

$stmt =  $db->stmt_init();

if ($stmt->prepare("INSERT INTO table (id, filePointer, amount, description) 
                        VALUES (?, ?, ?, ?)"))
{
    $id          = 0;
    $filePointer = '';
    $amount      = 0;
    $description = '';
    $stmt->bind_param('isis', $id, $filePointer, $amount, $description);

    foreach ($xml->machine as $machine) 
    {
        $id          = $machine->id;
        $filePointer = $machine->images->image->filePointer;
        $amount      = $machine->advertised_price->amount;
        $description = $machine->description;
        if (!$stmt->execute())
        { 
            echo "error : ".$id."<br />\n";
            break;
        }     
    }

    $stmt->close();
}
hakre
  • 193,403
  • 52
  • 435
  • 836
DNielsen
  • 17
  • 7

2 Answers2

1

$stmt returns false because the prepare is not successful.

your sql looks good except for the table name, so most likely your database connection is not up.

try this code after you made the conneciton:

if ($db->connect_errno) {
    printf("Connect failed: %s\n", $db->connect_error);
    exit();
}

and this one after prepare $stmt returns false because the prepare is not successful.

your sql looks good except for the table name, so most likely your database connection is not up.

try this code after you made the conneciton:

if (!$stmt) {
    printf("Errormessage: %s\n", $db->error);
}
The Surrican
  • 29,118
  • 24
  • 122
  • 168
1

Have you tried it with a $db->stmt_init() ?

$stmt =  $db->stmt_init();

if ($stmt->prepare("INSERT INTO table (id, filePointer, amount, description) VALUES (?, ?, ?, ?)")) {

....

$stmt->bind_param('ssss', $id, $filepointer, $amount, $description);

....
}

UPDATE :

or use 'i' corresponding variable has type integer and d for amount (Double) .

$stmt->bind_param('isds',  $machine->id, $machine->images->image->filePointer, $machine->advertised_price->amount, $machine->description);

UPDATE2 :

  • you should only execute one time bind_param (not inside the loop) !
  • you should use $stmt->close(); only outside the loop !

if ($stmt->prepare("INSERT INTO table (id, filePointer, amount, description) VALUES (?, ?, ?, ?)")) {
  $id = 0;
  $filePointer = '';
  $amount = 0.0;
  $description = '';
  $stmt->bind_param('isds', $id, $filePointer, $amount, $description);

  foreach ($xml->machine as $machine) {
    $id          = $machine->id;
    $filePointer = $machine->images->image->filePointer;
    $amount      = $machine->advertised_price->amount;
    $description = $machine->description;
    if (!$stmt->execute()) { 
        echo "error : ".$id."<br />\n";
        break;
       }     
    }

  $stmt->close();

UPDATE 3 :

  • you forgot a comma after description TEXT NOT NULL

$create_table =
'CREATE TABLE IF NOT EXISTS table  
(
    id INT NOT NULL,
    filePointer TEXT NOT NULL,
    amount INT NOT NULL,
    description TEXT NOT NULL
    PRIMARY KEY(id)
)';

Try this:

$create_tbl = "CREATE TABLE faulty(
      id INT NOT NULL,
      filePointer TEXT NOT NULL,
      amount INT NOT NULL,
      description TEXT NOT NULL,
      PRIMARY KEY(id)
      )";

if ($db->query($create_table) === TRUE) {
  echo 'Table "faulty" successfully created';
}
else {
 echo 'Error: '. $db->error;
}

$db->close();
moskito-x
  • 11,832
  • 5
  • 47
  • 60
  • Like this? I'm getting a syntax error, unexpected "{" $stmt = $db->stmt_init(); if ($stmt->prepare("INSERT INTO table (id, filePointer, amount, description) VALUES (?, ?, ?, ?)") { foreach ($xml->machine as $machine) { $stmt->bind_param('ssss', $id, $filepointer, $amount, $description); // Execute the query $stmt->execute(); } $stmt->close(); } – DNielsen Jun 13 '13 at 21:27
  • updated my answer forget one `)` – moskito-x Jun 13 '13 at 21:31
  • Looks like it works!! I think? I got no errors, but it doesn't seem as though my database has updated at all.. – DNielsen Jun 13 '13 at 21:36
  • is ID really a string ? – moskito-x Jun 13 '13 at 21:36
  • I've edited the code up there to be more accurate to where I'm at with it. I had made some changes earlier when I was messing with stuff that I'm reverting. I'm not sure whether it's a string or not - sorry I'm very new to php. – DNielsen Jun 13 '13 at 21:47
  • Has `id` a `AUTO_INCREMENT` attribute ? So you should use `NULL` : `VALUES (NULL, ?, ?, ?)` : and `$stmt->bind_param('sss', filepointer, $amount, $description);` – moskito-x Jun 13 '13 at 21:50
  • I've left all the attributes blank. None of them should have anything specified except they're of the type: Text. Gave that a try anyway but no dice. Other ideas? I would love to crack this. Thanks! – DNielsen Jun 13 '13 at 22:04
  • Can you show us how you create `table` ? Add it to your question. – moskito-x Jun 13 '13 at 22:07
  • look at my updated (2) answer . – moskito-x Jun 13 '13 at 23:04
  • Please do not give answers in a way to feed help vampires. Instead educate for better site use. – hakre Jun 17 '13 at 06:55