我收到此错误并且没有线索原因:
致命错误:在非对象
上调用成员函数bind_param()
$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();
}
}
以下是我如何调用表格:
//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!!";
}
以前我在MyPHPAdmin制作我的桌子。当我运行它然后检查我的数据库时,它不是创建表或更新它。我已经检查过以确保它连接到数据库,据我所知它正在工作。
我的新INSERT INTO代码:
$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();
}
答案 0 :(得分:1)
$ stmt返回false,因为prepare
未成功。
除了表名之外,你的sql看起来很好,所以很可能你的数据库连接没有启动。
在建立连接后尝试此代码:
if ($db->connect_errno) {
printf("Connect failed: %s\n", $db->connect_error);
exit();
}
准备好了这一个
$ stmt返回false,因为prepare
不成功。
除了表名之外,你的sql看起来很好,所以很可能你的数据库连接没有启动。
在建立连接后尝试此代码:
if (!$stmt) {
printf("Errormessage: %s\n", $db->error);
}
答案 1 :(得分:1)
您是否尝试使用$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);
....
}
更新:
或使用'i'对应变量的类型为整数,d
为金额(Double)。
$stmt->bind_param('isds', $machine->id, $machine->images->image->filePointer, $machine->advertised_price->amount, $machine->description);
UPDATE2:
bind_param
(不在循环内)!$stmt->close();
!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();
更新3:
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)
)';
试试这个:
$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();