我收到此错误,但不知道原因:
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();
}
}
以下是我调用表格的方法:
//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();
}
最佳答案
您是否尝试过使用 $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);
更新2:
- 您应该只执行一次
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();
关于mysql - 非对象上的bind_param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17096937/