mysql_connect

2009-05-31 14:53 / no comment / 51 views /

(PHP 4, PHP 5)打开一个到MySQL服务器的连接。

声明

resource _connect ([ string $server= ini_get(".default_host") [, string $username= ini_get(".default_user") [, string $password= ini_get(".default_password") [, bool $new_link= false [, int $client_flags= 0 ]]]]] )

打开或者重新使用一个到MySQL服务器连接。

参数

server
MySQL服务器。它也可以包含一个端口号,例如:”hostname:port”;或者给本地服务器提供一个到本地套接字的路径”:/path/to/”。
如果指示的mysql.default_host没有被定义,默认值就为’localhost:3306′。在SQL安全模式下,这个参数将被忽略而使用’localhost:3306′。

username
用户名。默认值由mysql.default_user定义。在SQL安全模式下,这个参数将被忽略而使用在使用这个服务器进程的用户。
password
密码。默认值由mysql.default_password定义。在SQL安全模式下,这个参数将被忽略而使用空密码。
new_link
如果使用相同的参数调用mysql_connect()两次,将不会建立新链接,而是用返回已经打开的连接。new_link更改这一行为,使得mysql_connect(),就算之前使用相同的参数调用过,总是打开新的连接。在SQL安全模式下,这个参数将被忽略。
client_flags
client_flags 参数可由以下常数组合而成:128 (使能 LOAD DATA LOCAL 处理), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE 或者 MYSQL_CLIENT_INTERACTIVE。阅读预先定义的常数获得更多信息。在SQL安全模式下,这个参数将被忽略。

返回值

成功则返回一个MySQL连接指示器,或者失败就返回FALSE。

更改日志

版本 描述
4.3.0 增加client_flags参数
4.2.0 增加the new_link参数

例子

Example #1 mysql_connect() example
<?php
$link = mysql_connect(‘localhost’, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>

Example #2 mysql_connect() example using hostname:port syntax
<?php
// we connect to example.com and port 3307
$link = mysql_connect(‘example.com:3307′, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);

// we connect to localhost at port 3307
$link = mysql_connect(’127.0.0.1:3307′, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>

Example #3 mysql_connect() example using “:/path/to/socket” syntax
<?php
// we connect to localhost and socket e.g. /tmp/mysql.sock

//variant 1: ommit localhost
$link = mysql_connect(‘:/tmp/mysql’, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);

// variant 2: with localhost
$link = mysql_connect(‘localhost:/tmp/mysql.sock’, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>

注意

1、当你指定”localhost”或者”localhost:port”做为服务器的话,MySQL客户端将会重写它并尝试连接一个本地套接字。如果你想使用TCP/IP,就要用”127.0.0.1″代替”localhost”。如果MySQL客户端尝试连接一个错误的本地套接字,你应该设置一个正确的路径来配置你的PHP,并且把服务器留白。
2、一旦脚本运行完,到服务器的连接就将被关闭,除非它被mysql_close()函数显示调用提前关闭了。
3、你可以在函数名前加上@来阻止错误消息。
4、错误信息”Can’t create TCP/IP socket (10106)”通常意味着可变指令配置不包含字母E。在Windows下,如果相关目录没有复制到系统环境变量中,PHP在加载Winsock的时候就会出错。

同样请看

* mysql_pconnect() – 打开一个持久连接
* mysql_close() – 关闭MySQL连接

Related Posts

  1. mysql_close
  2. mysql_create_db
  3. mysql_drop_db
  4. mysql_affected_rows
  5. mysql_error
  6. mysql_query
  7. mysql_num_rows
  8. mysql_db_query
  9. mysql_errno
  10. mysql_data_seek

Tags: , .

Get a Trackback link

No Comments Yet

You can be the first to comment!

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>