Db类下创建数据库连接方法和查询方法
`<?php
class Db {
public $dsn;
public $user;
public $password;
public $pdo;
public function connect(){
$this->pdo = new PDO($this->dsn,$this->user,$this->password);
}
public function __construct($dsn,$user,$password)
{
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
$this->connect();
}
public function __destruct()
{
$this->pdo = null;
}
public function select($sql){
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam('uid',$uid,PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($user as $v){
echo '<pre>' . print_r($v, true);
}
return $stmt;
}
}
$db = new Db(‘mysql:host=localhost;dbname=apple;’,’root’,’root’);
if ($db->pdo){
echo ‘数据库连接成功!’;
}
echo ‘<hr>‘;
$sql = ‘SELECT * FROM user
WHERE uid
>=1’;
$user = $db->select($sql);
print_r($user);
`