PHP mysqli error() 函数
示例 - 面向对象的样式
返回最近调用函数的最后一个错误描述:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Perform a query, check for error
if (!$mysqli -> query("INSERT INTO Persons (FirstName) VALUES ('Glenn')")) {
echo("Error description: " . $mysqli -> error);
}
$mysqli -> close();
?>
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Perform a query, check for error
if (!$mysqli -> query("INSERT INTO Persons (FirstName) VALUES ('Glenn')")) {
echo("Error description: " . $mysqli -> error);
}
$mysqli -> close();
?>
查看底部的程序样式示例。
定义和用法
error / mysqli_error() 函数返回最近调用函数的最后一个错误描述。
语法
面向对象的风格:
$mysqli -> error
程序风格:
mysqli_error(connection)
参数值
参数 | 描述 |
---|---|
connection | 必需。规定要使用的 MySQL 连接。 |
技术细节
返回值: | 返回一个带有错误描述的字符串。如果没有错误发生则返回 ""。 |
---|---|
PHP 版本: | 5+ |
Example - Procedural Oriented style
返回最近调用函数的最后一个错误描述:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')")) {
echo("Error description: " . mysqli_error($con));
}
mysqli_close($con);
?>
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')")) {
echo("Error description: " . mysqli_error($con));
}
mysqli_close($con);
?>