0

我收到此错误并且不知道原因:

致命错误:在非对象上调用成员函数 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();
}
4

2 回答 2

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);
}
于 2013-06-13T21:02:22.257 回答
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);

更新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();
于 2013-06-13T21:19:28.153 回答