当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > mysql_connect() 函数
The mysql_connect() function opens a non-persistent MySQL connection.
mysql_connect()函数的作用是:打开一个到 MySQL 服务器的连接。
This function returns the connection on success, or FALSE and an error on failure. You can hide the error output by adding an '@' in front of the function name.
该函数如果成功执行,将返回这个连接,如果失败,将返回False。当然你也可以在函数名称前面添加一个“@”来隐藏产生的错误。
mysql_connect(server,user,pwd,newlink,clientflag) |
Parameter 参数 | Description 描述 |
---|---|
server | Optional. Specifies the server to connect to (can also include a port number. e.g. "hostname:port" or a path to a local socket for the localhost). Default value is "localhost:3306" 可选参数。指定需要连接的服务器(他可以包含一个端口号。案例:“hostname:port”,或者一个连接到本地服务器的路径)。默认值为:“localhost:3306” |
user | Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process 可选参数。指定登陆的用户名。默认的用户名是该服务器拥有者的名字 |
pwd | Optional. Specifies the password to log in with. Default is "" 可选参数。指定登陆的密码。默认值为"" |
newlink | Optional. If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned 可选参数。如果你使用相同的自变量对mysql_connect()函数发出第二次请求,那么,它将建立一个新的连接;同样,它将返回已经打开的连接 |
clientflag | Optional. Can be a combination of the following constants: 可选参数。可以使用的值如下:
|
Tip: The connection will be closed as soon as the script ends. To close the connection before, use mysql_close().
提示:但脚本程序结束运行后,连接将被关闭;你可以使用mysql_close()函数关闭这个连接。
Tip: To establish a persistent MySQL connection, use mysql_pconnect() instead.
提示:你可以使用mysql_pconnect()函数建立一个持久的MySQL连接。
<?php $con = mysql_connect("localhost","mysql_user","mysql_pwd"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code mysql_close($con); ?> |