<?php
class mssql2008
{
private $db; //数据库操作对象
//数据库信息
private $db_info=array(
'server'=>'localhost',
'user'=>'Jimxxx',
'password'=>sa',
'databases'=>'databasename'
);
//初使化连接数据库
public function __construct($info=array())
{
$db_info = array("UID"=>$info['user'], "PWD"=>$info['password'], "Database"=>$info['databases']);
$db = sqlsrv_connect( $info['server'], $db_info);
if($db===false){
echo "data false!";
die( print_r( sqlsrv_errors(), true));
}else{
$this->db_info = $info;
$this->db = $db;
}
}
//执行sql语句
public function query($sql='')
{
$sql = trim($sql);
if(!isset($this->db) || $sql==''){
return array();
}
$query = sqlsrv_query($this->db,$sql);
if($query === false){
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}else{
return $query;
}
}
//查询记录
public function getrs($sql='')
{
$query = $this->query($sql);
$rs = array();
//遍历出记录集
while($row = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC)){
$rs[] = $row;
}
return $rs;
}
//释放资源
public function close(){//多加了一个释放资源
sqlsrv_close( $this->db);
}
}
?>
<?php
//定义参数
$db_info=array(
'server'=>'sdf',
'user'=>'Jimdfsdfvities',
'password'=>sdfsdfsdfsdf/sdf==',
'databases'=>'Activities'
);
?>
从网上摘的,修改了一点点;
<?php
//数据库对象
$db = new mssql2008($db_info);
//取得记录
$arr_pro = $db->getrs($sql='SELECT * FROM mytest');
while (list($key,$value) = each($arr_pro)) {
echo "\"num:$key : $value[id] \"$value[name]\" <br>";
}
$db->close();//一定要释放资源
?>