The Object oriented style
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
?>
always returns a object which represents connection to a MYSQL,also connection faild , so the code below won't throw a Exception when connection faild:
<?php
try {
$db = new mysqli('localhost', 'userName', 'passWord', 'dbName');
if(!$db){
throw new Exception('connect databases faild!');
}
}
catch (Exception $e){
echo $e->getMessage();
}
?>
you must check connection with mysqli_connect_errno()
<?php
try {
$db = new mysqli('localhost', 'userName', 'passWord', 'dbName');
if(mysqli_connect_errno()){
throw new Exception('connect databases faild!');
}
}
catch (Exception $e){
echo $e->getMessage();
}
?>
mysqli::__construct
mysqli_connect
(PHP 5)
mysqli_connect — Open a new connection to the MySQL server
Description
Object oriented style (constructor):
Procedural style
Opens a connection to the MySQL Server running on.
Parameters
- host
-
Can be either a host name or an IP address. Passing the NULL value or the string "localhost" to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol.
- username
-
The MySQL user name.
- passwd
-
If not provided or NULL, the MySQL server will attempt to authenticate the user against those user records which have no password only. This allows one username to be used with different permissions (depending on if a password as provided or not).
- dbname
-
If provided will specify the default database to be used when performing queries.
- port
-
Specifies the port number to attempt to connect to the MySQL server.
- socket
-
Specifies the socket or named pipe that should be used.
Note: Specifying the socket parameter will not explicitly determine the type of connection to be used when connecting to the MySQL server. How the connection is made to the MySQL database is determined by the host parameter.
Return Values
Returns a object which represents the connection to a MySQL Server or FALSE if the connection failed.
Examples
Example #1 Object oriented style
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", $mysqli->host_info);
/* close connection */
$mysqli->close();
?>
Example #2 Procedural style
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", mysqli_get_host_info($link));
/* close connection */
mysqli_close($link);
?>
The above example will output:
Host information: Localhost via UNIX socket
Notes
Note: Error "Can't create TCP/IP socket (10106)" usually means that the variables_order configure directive doesn't contain character E. On Windows, if the environment is not copied the SYSTEMROOT environment variable won't be available and PHP will have problems loading Winsock.
mysqli::__construct
25-May-2008 09:10
28-Jan-2008 12:52
To specify charset in my.cnf file you just have to add
skip-character-set-client-handshake directive after
[mysqld]
default-character-set=utf8
30-Jul-2007 05:53
When you need more(less) timeout, you can use this. This only guarantees that host and port are opened. It is suitable on the very high speed networks(LAN) and where one second is not acceptable.
$timeoutInSeconds can be set to any numeric value
e.g. "1/2", "0.500","0.1","0.01" etc.
--------------------------------------------------------------
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
$fp = fsockopen("$mysqlHost",$mysqlPort, $errno, $errstr,$timeoutInSeconds);
if (!$fp)
{
//timeout mostly
echo "ERR: $errno - $errstr<br>\n";
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Timeout $time sekund";
}
else
{
fclose($fp);
}
06-Jul-2007 02:29
This helped me:
<?php
$db = mysqli_connect($host, $login, $pass, $dbName);
$db->set_charset('utf8');
...
?>
18-Feb-2007 07:52
Yes, it's totally odd that php.ini doesn't seem to allow you to global specify the client connection as utf-8. You might try this in the
my.cnf file
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
It didn't work for me, but others may have better luck. In the end I had to specify in the php code as noted below.
31-Jan-2007 07:13
A quick word about the connection encoding for this extension. It doesn't appear to be documented anywhere that I can find, but this defaults to latin1 encoding for the connection, regardless of PHP's or MySQL's settings. I run in a completely UTF-8 setup, and spent days trying to discover the cause of corrupted characters from the Db. If, like me, you need to work with other than latin1, use the set_charset function/method to switch to your encoding of choice.
