Blogger Information
Blog 13
fans 0
comment 0
visits 9036
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Db类下创建数据库连接方法和查询方法
文永
Original
933 people have browsed it

Db类下创建数据库连接方法和查询方法

`<?php
class Db {
public $dsn;
public $user;
public $password;
public $pdo;

  1. public function connect(){
  2. $this->pdo = new PDO($this->dsn,$this->user,$this->password);
  3. }
  4. public function __construct($dsn,$user,$password)
  5. {
  6. $this->dsn = $dsn;
  7. $this->user = $user;
  8. $this->password = $password;
  9. $this->connect();
  10. }
  11. public function __destruct()
  12. {
  13. $this->pdo = null;
  14. }
  15. public function select($sql){
  16. $stmt = $this->pdo->prepare($sql);
  17. $stmt->bindParam('uid',$uid,PDO::PARAM_INT);
  18. $stmt->execute();
  19. $user = $stmt->fetchAll(PDO::FETCH_ASSOC);
  20. foreach ($user as $v){
  21. echo '<pre>' . print_r($v, true);
  22. }
  23. return $stmt;
  24. }

}

$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);
`

Correcting teacher:天蓬老师天蓬老师

Correction status:unqualified

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