mysqli::error() 未检测到

mysqli::error() is not detected

我是 php 的初学者,我有一个我自己无法解释的问题。我有两个文件:第一个 index.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
</head>
<body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
        <input name="file" type="file"/>
        <input type="submit" value="Wyslij" />
    </form>
</body>
</html>

和第二个upload.php:

<?php

if(isset($_FILES['file'])){
$db=new mysqli('localhost','root','kursphp','products');

    $name= $db->real_escape_string($_FILES['file']['name']);
    $type=$db->real_escape_string($_FILES['file']['type']);
    $data=$db->real_escape_string(file_get_contents($_FILES['file']['tmp_name']));
    $size=intval($_FILES['file']['size']);

    //question

    $query= "INSERT INFO files(name,type,size,data,created) VALUES ('{$name}','{$type}','{$size}','{$data}',NOW())";

    $result=$db->query($query);

    if($result){
    echo "File saved";
    } else {
        echo $db->error();
    }

    $db->close();
}

?>

当我上传一些文件时,出现错误:

Fatal error: Call to undefined method mysqli::error() in D:\wamp\www\php_z\upload\upload.php on line 20

我不知道为什么会出现这种错误,因为正如我声明的那样:db 是 mysqli 的一个对象。我在 PHP 的文档中读到

mysqli::$error -- mysqli_error — Returns a string description of the last error

所以有这样的方法error()..有人能告诉我如何解决这个问题吗?

它不是一个函数,它是一个变量。像这样访问它:

echo $db->error;

Read More

没有名为错误的方法。有一个名为错误的变量。因此,更改

echo $db->error();

echo $db->error;

你有一些错误。

第一个是:

INSERT INFO files
         ^

应该读作 INTO 而不是 INFOTF 在键盘上彼此靠近,因此可能会发生这种情况。

然后是 echo $db->error();,应该读作 echo $db->error;

这应该是面向对象的风格,而不是过程风格。