1. 程式人生 > >將封裝的資料庫類,改寫成靜態屬性和方法來訪問

將封裝的資料庫類,改寫成靜態屬性和方法來訪問

<?php
class static_db{
private static $host;
private static $username;
private static $password;
private static $db_name;
private static $link;
public function __construct($host,$username,$password,$db_name){
self::$host=$host;
self::$username=$username;
self::$password=$password;
self::$db_name=$db_name;
self::conncet();
}
public static function conncet(){
self::$link=mysql_connect(self::$host,self::$username,self::$password);
mysql_select_db(self::$db_name);
mysql_query('set names utf8');
}
public function __destruct(){
mysql_close(self::$link);
}
public function get_rows($table_name){
$sql="select * from $table_name";
$res=mysql_query($sql);
$array=array();
while($rows=mysql_fetch_assoc($res)){
$array[]=$rows;

}
return $array;
}
}
?>