abstract:$type = 'mysql';//数据库类型 $host = '127.0.0.1'; //数据库主机名 $dbname = 'php_edu';//数据库名 $charset = 'utf8'; //默认编码 $port
$type = 'mysql';//数据库类型 $host = '127.0.0.1'; //数据库主机名 $dbname = 'php_edu';//数据库名 $charset = 'utf8'; //默认编码 $port = 3306; //端口默认值3306 //使用字符串拼接出DSN格式 $dsn = $type.':host='.$host.';dbname='.$dbname.';charset='.$charset.';port='.$port; $user = 'root';//数据库用户名 $pass = 'root';//用户密码 //错误抛出代码 try{ //链接 $pdo = new PDO($dsn,$user,$pass); echo $pdo ? '链接成功' : '连接失败'; }catch (PDOException $e){ exit($e->getMessage());//错误抛出系统提示代码 }
Correcting teacher:天蓬老师Correction time:2019-03-29 14:21:39
Teacher's summary:pdo连接数据库, 可以写得非常复杂,也可以写得非常简单, 还是要看场景吧