Blogger Information
Blog 27
fans 2
comment 0
visits 19691
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的实战自动连接数据库---PHP九期线上班
一丁
Original
804 people have browsed it

连接数据库


实例

<?php
namespace yiding;
class Db{
	public $pdo;
	public function __construct($dsn,$user,$password){
		$this->pdo=new \PDO($dsn,$user,$password);
	}
	public function __destruct(){
		echo '連接已斷開';
	}
}
$db=new Db('mysql:host=localhost;dbname=test','root','root');
if($db->pdo){
	echo '連接成功','<br>';
}

$stmt=$db->pdo->prepare('SELECT * FROM `st`');
$stmt->execute();
foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $c){
	print_r($c);
	echo '<br>';
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

运行图:

image.png


手写代码:

image.png

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:看来你比较喜欢繁体字, 不过, 作业中用了简体字, 辛苦了, 完成的不错, 作业用了色纸, 看来是不计成本的, 下了血本要学会编程
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post