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();
}
Have you tried it with a
$db->stmt_init()
?UPDATE :
or use 'i' corresponding variable has type integer and
d
for amount (Double) .UPDATE2 :
bind_param
(not inside the loop) !$stmt->close();
only outside the loop !UPDATE 3 :
description TEXT NOT NULL
Try this:
$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:
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: