PHP mySQLi 出错

PHP Error with mySQLi

我正在学习PHP和MySQL并制作了一个小网站来帮助我学习。在我实施 MySQLi 时,我不断收到代码错误。我尝试了各种尝试,比如尝试查询所有表的内容,但我得到 0 行,即使我已经有一些。我正在使用 WAMP Server 2.5 并且连接成功,但其他任何东西似乎都不起作用。 当我使用代码时:

$sql = "USE mydb
INSERT INTO persons (Name, User, Pass)
VALUES ('John', 'Doe', 'johnexample');";
if ($conn->query($sql) === TRUE){
    echo "Success";
}else{
    echo "Error, Please Try Again Later<br>".$conn->error;
}
$conn->close();

我很惊讶这不起作用,因为它与 W3 Schools 网站上的示例几乎完全相同。我看过其他有此错误的帖子,但他们的解决方案不起作用。我得到的错误是:

You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'INSERT INTO
persons (Name, User, Pass) VALUES ('John', 'Doe', 'johnexample')' at line 2

感谢任何帮助!谢谢!

请注意字段名区分大小写!请检查Name、User、Pass是否真的以大写字母开头。

问题是 mysqli::query 不支持多个查询,即使支持,也需要 ; 终止符。这会导致您的 SQL 语法错误。

如果要更改连接的当前数据库,可以使用:

$conn->select_db('mydb');

然后您可以执行不带 USE 部分的 INSERT 查询,这将应用于 mydb