mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
* Provide OOP example when extending mysqli class.
* Adjust examples to match other connect examples. * Check for errors along the way. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@277081 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
b51dded646
commit
641729d204
1 changed files with 68 additions and 31 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.2 $ -->
|
||||
<!-- $Revision: 1.3 $ -->
|
||||
<refentry xml:id="mysqli.real-connect" xmlns="http://docbook.org/ns/docbook">
|
||||
<refnamediv>
|
||||
<refname>mysqli::real_connect</refname>
|
||||
|
@ -199,26 +199,62 @@
|
|||
<![CDATA[
|
||||
<?php
|
||||
|
||||
/* create a connection object which is not connected */
|
||||
$mysqli = mysqli_init();
|
||||
|
||||
/* set connection options */
|
||||
$mysqli->options(MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0");
|
||||
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
|
||||
|
||||
/* connect to server */
|
||||
$mysqli->real_connect('localhost', 'my_user', 'my_password', 'world');
|
||||
|
||||
/* check connection */
|
||||
if (mysqli_connect_errno()) {
|
||||
printf("Connect failed: %s\n", mysqli_connect_error());
|
||||
exit();
|
||||
if (!$mysqli) {
|
||||
die('mysqli_init failed');
|
||||
}
|
||||
|
||||
printf ("Connection: %s\n.", $mysqli->host_info);
|
||||
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();
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
<example>
|
||||
<title>Object oriented style when extending mysqli class</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?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();
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
|
@ -228,23 +264,25 @@ $mysqli->close();
|
|||
<![CDATA[
|
||||
<?php
|
||||
|
||||
/* create a connection object which is not connected */
|
||||
$link = mysqli_init();
|
||||
|
||||
/* set connection options */
|
||||
mysqli_options($link, MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0");
|
||||
mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
|
||||
|
||||
/* connect to server */
|
||||
mysqli_real_connect($link, 'localhost', 'my_user', 'my_password', 'world');
|
||||
|
||||
/* check connection */
|
||||
if (mysqli_connect_errno()) {
|
||||
printf("Connect failed: %s\n", mysqli_connect_error());
|
||||
exit();
|
||||
if (!$link) {
|
||||
die('mysqli_init failed');
|
||||
}
|
||||
|
||||
printf ("Connection: %s\n.", mysqli_get_host_info($link));
|
||||
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);
|
||||
?>
|
||||
|
@ -254,8 +292,7 @@ mysqli_close($link);
|
|||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
Connection: Localhost via UNIX socket
|
||||
|
||||
Success... MySQL host info: localhost via TCP/IP
|
||||
]]>
|
||||
</screen>
|
||||
</refsect1>
|
||||
|
|
Loading…
Reference in a new issue