代码如下:
有兴趣的可以测试下
摘自于某书
<? php public function dbConnect( $usertype, $connectionType = 'mysqli' ) {
$host = 'localhost';
$db = 'phpsols';
if( $usertype == 'read') {
$user = 'psread';
$pwd = '123456';
} else if ( $usertype == 'write' ) {
$user = 'pswrite';
$pwd = '123456';
} else {
exit( 'Unrecognized connection type' );
}
if( $connectionType == 'mysqli') {
return new mysqli( $host, $user, $pwd, $db ) or die( 'Cannot open database' );
} else {
try {
return new PDO( "mysql:host=$host;dbname=$db", $user, $pwd );
} catch( PDOException $e ) {
$e -> getMessage;
echo 'Cannot connect to database';
exit;
}
}
} ?>