/**
* MyClass 抽象类,用于执行查询语句
*
*/
class MyClass
{
const HOST = '192.168.73.110:3306';
const USER = 'root';
const PASSWORD = 'root';
const DB = 'kmdbcenter';
static $Instance = false;
private $QueryResult = False;
private final function __construct()
{
if(!@mysql_connect(MyClass::HOST, MyClass::USER, MyClass::PASSWORD))
{
die('connection failed');
}
if(!@mysql_select_db(MyClass::DB))
{
die('connected');
}
}
public static function GetInstance()
{
if(empty(self::$Instance))
{
$Class = __CLASS__;
self::$Instance = new $Class;
}
return self::$Instance;
}
/**
* MySql查询方法
*/
public function query($SqlStr)
{
return $this->QueryResult = @mysql_query($SqlStr);
}
/**
* 返回查询到的值(数组的形式)
*/
public function GetArray()
{
$Date = array();
while($Row = mysql_fetch_assoc($this->QueryResult))
{
$Date[] = $Row;
}
return $Date;
}
}
?>
2、调用的方法(index.php)
require_once 'MyClass.php';
$Instance = MyClass::GetInstance();
$Instance->Query('select * from km_tbl_songster');
print_r($Instance->GetArray());
?>