数据库 test ,user 表结构:
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) CHARACTER SET utf8 DEFAULT NULL, `mobile` varchar(11) CHARACTER SET utf8 DEFAULT NULL, `regtime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
Common\User.php:
db = new Database\MySQLi();
$conn = $this->db->connect('127.0.0.1', 'root', '', 'test');
$res = $this->db->query("select * from user where id = {$id} limit 1");
$data = $res->fetch_assoc();
$this->id = $data['id'];
$this->name = $data['name'];
$this->mobile = $data['mobile'];
$this->regtime = $data['regtime'];
}
//析构方法
function __destruct() {
$this->db->query("update user set name = '{$this->name}', mobile = '{$this->mobile}', regtime = '{$this->regtime}' where id = {$this->id} limit 1");
}
}Common\Databases\MySQLi.php
conn = $conn;
}
function query($sql){
$res = mysqli_query($this->conn, $sql);
return $res;
}
function close(){
mysqli_close($this->conn);
}
}入口文件 index.php
';
/*
* 对对象属性的操作就完成了对数据库的操作
*/
$user = new Common\User(1);
//读取数据
var_dump($user->id, $user->mobile, $user->name, $user->regtime);exit();
$user->mobile = '13800138000';
$user->name = 'Arshavin';
$user->regtime = date("Y-m-d H:i:s",time());
上一篇:php调用python文件的方法
下一篇:怎么在PHP中定义模版引擎