(PHP 5, PHP 7)
mysqli::real_connect -- mysqli_real_connect — ����һ�� MySQL ����������
���������
$host
[, string $username
[, string $passwd
[, string $dbname
[, int $port
[, string $socket
[, int $flags
]]]]]]] ) : bool���̻����
$link
[, string $host
[, string $username
[, string $passwd
[, string $dbname
[, int $port
[, string $socket
[, int $flags
]]]]]]] ) : bool����һ���� MySQL �����������ӡ�
�� mysqli_connect() �IJ�ͬ�㣺
mysqli_real_connect() ��Ҫһ����Ч�Ķ������������ mysqli_init() ������
����ʹ�� mysqli_options() ���ø����������á�
�ṩ flags
������
link
���Թ��̻���ʽ����mysqli_connect() �� mysqli_init() ���ص����ӱ�ʶ��
host
����ʹ��������IP ��ַ��������� NULL
�����ַ��� "localhost" ��ô��ʹ��
ͨ����� TCP/IP ���ӱ��ط�������
username
MySQL ��¼�û���
passwd
������� NULL
����ô��ʹ��û��������֤�ķ�ʽ���Ե�¼����������Ϊһ���û�
�ṩ��ͬ��Ȩ�ޣ��������Ƿ��ṩ�����롣
dbname
����ִ�в�ѯ����Ĭ�����ݿ⡣
port
ָ�� MySQL �������Ķ˿�
socket
ָ��ʹ�õ� socket ��������ͨ����
Note:
ָ��
socket
����������˵��Ҫ���ú��ַ�ʽ�������ݿ⡣ �������ݵķ�ʽ��host
�趨��
flags
��������������Ӳ�����
Name | Description |
---|---|
MYSQLI_CLIENT_COMPRESS |
ʹ��ѹ��Э�� |
MYSQLI_CLIENT_FOUND_ROWS |
�������ƥ���������������Ӱ������� |
MYSQLI_CLIENT_IGNORE_SPACE |
���������ƺ��пո��⽫ʹ���еĺ������Ƴ�Ϊ�����֡� |
MYSQLI_CLIENT_INTERACTIVE |
�ڹر�����֮ǰ����ȴ� interactive_timeout �룬 ����� wait_timeout �趨�� |
MYSQLI_CLIENT_SSL |
ʹ�� SSL ���� |
Note:
�Ӱ�ȫ�Ƕȿ��ǣ��� PHP �в�����ʹ��
MULTI_STATEMENT
�� ��Ҫִ�ж��ѯ��䣬��ʹ�� mysqli_multi_query()��
�ɹ�ʱ���� TRUE
�� ������ʧ��ʱ���� FALSE
��
Example #1 mysqli::real_connect() ����
���������
<?php
$mysqli = mysqli_init();
if (!$mysqli) {
die('mysqli_init failed');
}
if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!$mysqli->real_connect('localhost', 'my_user', 'my_password', 'my_db')) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . $mysqli->host_info . "\n";
$mysqli->close();
?>
��������� when extending mysqli class
<?php
class foo_mysqli extends mysqli {
public function __construct($host, $user, $pass, $db) {
parent::init();
if (!parent::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!parent::real_connect($host, $user, $pass, $db)) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
}
$db = new foo_mysqli('localhost', 'my_user', 'my_password', 'my_db');
echo 'Success... ' . $db->host_info . "\n";
$db->close();
?>
���̻����
<?php
$link = mysqli_init();
if (!$link) {
die('mysqli_init failed');
}
if (!mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!mysqli_real_connect($link, 'localhost', 'my_user', 'my_password', 'my_db')) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
?>
�������̻������
Success... MySQL host info: localhost via TCP/IP
Note:
MySQLnd ����ʹ�÷�������Ĭ���ַ��������ַ������������֣���֤ʱ���ͣ����� mysqlnd ʹ�á�
Libmysqlclient ʹ�� my.cnf �е�Ĭ���ַ����������ڵ��� mysqli_init() ֮��mysqli_real_connect() ֮ǰ�ȵ��� mysqli_options() ��ָ����