Fatal error: Call to undefined method mysqli::erro

2020-01-29 17:30发布

I can connect but when it comes to the prepared statement that is the error i got. anything wrong there?

Code:

    // Open connection
    $db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

    //check connection
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    // Create statement object
    $stmt = $db->stmt_init();   

    // sql statement
    $sql = "INSERT INTO ch_users ('uid','admin','password','directory','size','format','locationid') VALUES (?, 1, ?, ?, ?, ?, 1)";

    // Create a prepared statement
    $stmt = $db->prepare($sql) or die($db->error());

标签: php mysqli
2条回答
老娘就宠你
2楼-- · 2020-01-29 18:10

My bad, there shouldn't be quotes in the sql statements for column fields. Now it works!

查看更多
The star\"
3楼-- · 2020-01-29 18:36

For those who stumbled on this page.

The error you get from

Fatal error: Call to undefined method mysqli::error()

This is because we need to access the error as variable not as function

so the correct code is

if(! empty( $mysqli->error ) ){
   echo $mysqli->error;  // <- this is not a function call error()
}

Hope this helps someone.

查看更多
登录 后发表回答