bind_param on a non-object [closed]

六月ゝ 毕业季﹏ 提交于 2019-12-25 11:18:12

问题


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();
}

回答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();



回答2:


$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);
}


来源:https://stackoverflow.com/questions/17096937/bind-param-on-a-non-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!