pdo 连接数据库

dsn=mysql:dbname=demos;host=127.0.0.1;port=3306; d s n = ‘ m y s q l : d b n a m e = d e m o s ; h o s t = 127.0.0.1 ; p o r t = 3306 ′ ; username = ‘root’;
password=root;try$pdo=newPDO($dsn,$username,$password);$pdo>exec(setnamesutf8);$pdo>setAttribute(PDO::ATTRDEFAULTFETCHMODE,PDO::FETCHASSOC);catch(PDOException p a s s w o r d = ‘ r o o t ′ ; t r y $ p d o = n e w P D O ( $ d s n , $ u s e r n a m e , $ p a s s w o r d ) ; $ p d o − > e x e c ( ‘ s e t n a m e s u t f 8 ′ ) ; $ p d o − > s e t A t t r i b u t e ( P D O :: A T T R D E F A U L T F E T C H M O D E , P D O :: F E T C H A S S O C ) ; c a t c h ( P D O E x c e p t i o n e) {
die(‘Could not connect to the database:’ . $e);
}

你可能感兴趣的:(开发)