摘要://pdo對象 $type = 'mysql';//數(shù)據(jù)庫類型 $host = '127.0.0.1';//主機名 $dbname = 'test';//數(shù)據(jù)庫名 $charset = 'utf8';//字符集 //$port = 33
//pdo對象 $type = 'mysql';//數(shù)據(jù)庫類型 $host = '127.0.0.1';//主機名 $dbname = 'test';//數(shù)據(jù)庫名 $charset = 'utf8';//字符集 //$port = 3306;//端口 $dsn = $type . ':host=' . $host . ';dbname=' . $dbname . ';charset=' . $charset; $user = 'root';//用戶名 $pass = '123456';//密碼 try{ //連接數(shù)據(jù)庫 $pdo = new \PDO($dsn,$user,$pass); //關(guān)閉數(shù)據(jù)庫。默認在代碼執(zhí)行完畢后會自動關(guān)閉。 $pdo = null; // unset($pdo); var_dump($pdo); }catch (PDOException $e){ exit($e->getMessage()); }
批改老師:查無此人批改時間:2018-11-27 15:23:05
老師總結(jié):寫的不錯,可以把連接數(shù)據(jù)庫寫成方法,每次用就方便了。