note 83419 added to mysqli.connect
| From: | lgz2648 at 163 dot com | Date: | Sun, 25 May 2008 08:10:19 +0000 |
| Subject: | note 83419 added to mysqli.connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139722@lists.php.net to get a copy of this message | ||
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();
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 121.32.93.150
----
Manual Page -- http://www.php.net/manual/en/mysqli.connect.php
Edit -- https://master.php.net/note/edit/83419
Del: integrated -- https://master.php.net/note/delete/83419/integrated
Del: useless -- https://master.php.net/note/delete/83419/useless
Del: bad code -- https://master.php.net/note/delete/83419/bad+code
Del: spam -- https://master.php.net/note/delete/83419/spam
Del: non-english -- https://master.php.net/note/delete/83419/non-english
Del: in docs -- https://master.php.net/note/delete/83419/in+docs
Del: other reasons-- https://master.php.net/note/delete/83419
Reject -- https://master.php.net/note/reject/83419
Search -- https://master.php.net/manage/user-notes.php