将数据库常用的操作(连接数据库,获得所有数据,获得一条记录,获得一列记录,获得一条索引数组,获得一条关联数组)都给封装到db.class.php里面,谁需要谁继承这个类 注意:提交封装的类文件

来源:互联网 发布:传奇霸业魂珠暴击数据 编辑:程序博客网 时间:2024/04/28 11:25
<?php
class dbname{
private $localhost;
private $root;
private $pass;
private $db_name;
public function __construct($localhost,$root,$pass,$db_name){
$this->localhost=$localhost;
$this->root=$root;
$this->pass=$pass;
$this->db_name=$db_name;
$this->query();
}
private function query(){
mysql_connect($this->localhost,$this->root,$this->pass);
mysql_select_db($this->db_name);
mysql_query("set names utf8");
}
public function getAll($sql){
$result = mysql_query($sql);
$rows=array();
while($row = mysql_fetch_assoc($result)){//返回的是多条记录
$rows[]=$row;


}
return $rows;
}