PHP程序 mysql_errno() 函数和mysql_close函数

int mysql_errno ( [resource link_identifier] )

返回上一个 MySQL 函数的错误号码,如果没有出错则返回 0(零)。

从 MySQL 数据库后端来的错误不再发出警告,要用 mysql_errno() 来提取错误代码。注意本函数仅返回最近一次 MySQL 函数的执行(不包括 mysql_error() 和 mysql_errno())的错误代码,因此如果要使用此函数,确保在调用另一个 MySQL 函数之前检查它的值。

resource link_identifier 可选。规定 SQL 连接标识符。如果未规定,则使用上一个打开的连接。

例子

在本例中,我们将尝试使用错误的用户名和密码来登录一个 MySQL 服务器:

 

<?php

$con = mysql_connect("localhost","wrong_user","wrong_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_errno());
}
mysql_close($con);
?>

 

输出类似:

Could not connect: 1045

 

 

mysql_close -- 关闭 MySQL 连接

说明
bool mysql_close ( [resource link_identifier] )

如果成功则返回 TRUE,失败则返回 FALSE。

mysql_close() 关闭指定的连接标识所关联的到 MySQL 服务器的连接。如果没有指定 link_identifier,则关闭上一个打开的连接。

通常不需要使用 mysql_close(),因为已打开的非持久连接会在脚本执行完毕后自动关闭。

注: mysql_close() 不会关闭由 mysql_pconnect() 建立的持久连接。

例子 1. MySQL 关闭例子

<?php

$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect: " . mysql_error());
print ("Connected successfully");
mysql_close($link);
?>