MYSQL简单的封装
来源:互联网 发布:应对网络诈骗防范措施 编辑:程序博客网 时间:2024/06/06 06:58
<?php
class Mysql {
private$host;
private$user;
private$pwd;
private$dbName;
private$charset;
private$conn = null; // 保存连接的资源
publicfunction __construct() {
// 应该是在构造方法里,读取配置文件
// 然后根据配置文件来设置私有属性
// 此处还没有配置文件,就直接赋值
$this->host = 'localhost';
$this->user = 'root';
$this->pwd = '111111';
$this->dbName = 'test';
// 连接
$this->connect($this->host,$this->user,$this->pwd);
// 切换库
$this->switchDb($this->dbName);
// 设置字符集
$this->setChar($this->charset);
}
//负责连接
privatefunction connect($h,$u,$p) {
$conn = mysql_connect($h,$u,$p);
$this->conn = $conn;
}
//负责切换数据库,网站大的时候,可能用到不止一个库
publicfunction switchDb($db) {
$sql = 'use ' . $db;
$this->query($sql);
}
//负责设置字符集
publicfunction setChar($char) {
$sql = 'set names ' . $char;
$this->query($sql);
}
//负责发送sql查询
publicfunction query($sql) {
return mysql_query($sql,$this->conn);
}
//负责获取多行多列的select 结果
publicfunction getAll($sql) {
$list = array();
$rs = $this->query($sql);
if(!$rs) {
return false;