连接数据库
实例
<?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>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
运行图:
手写代码: