Definition and Usage
定义和用法
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。当然你也可以在函数名称前面添加一个“@”来隐藏产生的错误。
Syntax
语法
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:
可选参数。可以使用的值如下:
MYSQL_CLIENT_SSL - Use SSL encryption
MYSQL_CLIENT_SSL – 使用SSL技术加密
MYSQL_CLIENT_COMPRESS - Use compression protocol
MYSQL_CLIENT_COMPRESS – 使用压缩[compression]协议
MYSQL_CLIENT_IGNORE_SPACE - Allow space after function names
MYSQL_CLIENT_IGNORE_SPACE – 允许在函数名称后面出现空格
MYSQL_CLIENT_INTERACTIVE - Allow interactive timeout seconds of inactivity before closing the connection
MYSQL_CLIENT_INTERACTIVE – 在关闭连接之前,允许对休止状态下的函数设置激活时间
Tips and Notes
提示
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连接。
Example
案例
<?php
$con = mysql_connect("localhost","mysql_user","
mysql_pwd");
if (!$con) { die('Could not connect: ' . mysql_error()); }
// some code
mysql_close($con);?>
--转自