最近工作上遇到问题,甲方数据存在sqlservel数据库中,我们需要用php去连接他们的数据库获取数据。我自己写的封装方法,使用时只需实例化对象,即可使用类文件中的方法。
SQLSRV函数
sqlsrv_begin_transaction — Begins a database transaction 注释:开始数据库事务
sqlsrv_cancel — Cancels a statement 注释:取消一个声明
sqlsrv_client_info — Returns information about the client and specified connection 注释:返回有关客户端和指定连接的信息
sqlsrv_close — Closes an open connection and releases resourses associated with the connection 注释:关闭打开的连接并释放与该连接关联的资源
sqlsrv_commit — Commits a transaction that was begun with sqlsrv_begin_transaction 注释:提交以sqlsrv_begin_transaction开始的事务
sqlsrv_configure — Changes the driver error handling and logging configurations 注释:更改驱动程序错误处理和日志配置
sqlsrv_connect — Opens a connection to a Microsoft SQL Server database 注释:打开到Microsoft SQL Server数据库的连接
sqlsrv_errors — Returns error and warning information about the last SQLSRV operation performed 注释:返回关于上次执行的SQLSRV操作的错误和警告信息
sqlsrv_execute — Executes a statement prepared with sqlsrv_prepare 注释:执行使用sqlsrv_prepare准备的语句
sqlsrv_fetch_array — Returns a row as an array 注释:以数组形式返回一行
sqlsrv_fetch_object — Retrieves the next row of data in a result set as an object 注释:检索结果集中作为对象的下一行数据
sqlsrv_fetch — Makes the next row in a result set available for reading 注释:使结果集中的下一行可供读取
sqlsrv_field_metadata — Retrieves metadata for the fields of a statement prepared by sqlsrv_prepare or sqlsrv_query 注释: 检索sqlsrv_prepare或sqlsrv_query准备的语句的字段的元数据
sqlsrv_free_stmt — Frees all resources for the specified statement 注释:释放指定语句的所有资源
sqlsrv_get_config — Returns the value of the specified configuration setting 注释:返回指定配置设置的值
sqlsrv_get_field — Gets field data from the currently selected row 注释:从当前选定的行获取字段数据
sqlsrv_has_rows — Indicates whether the specified statement has rows 注释:指示指定语句是否具有行
sqlsrv_next_result — Makes the next result of the specified statement active 注释:使指定语句的下一个结果活动
sqlsrv_num_fields — Retrieves the number of fields (columns) on a statement 注释:检索语句上的字段(列)的数量
sqlsrv_num_rows — Retrieves the number of rows in a result set 注释:检索结果集中的行数
sqlsrv_prepare — Prepares a query for execution 注释:为执行准备查询
sqlsrv_query — Prepares and executes a query 注释:准备并执行查询
sqlsrv_rollback — Rolls back a transaction that was begun with sqlsrv_begin_transaction 注释:回滚以sqlsrv_begin_transaction开始的事务
sqlsrv_rows_affected — Returns the number of rows modified by the last INSERT, UPDATE, or DELETE query executed 注释:返回上次执行的插入、更新或删除查询修改的行数
sqlsrv_send_stream_data — Sends data from parameter streams to the server 注释:将数据从参数流发送到服务器
sqlsrv_server_info — Returns information about the server 注释:返回关于服务器的信息
date_default_timezone_set('PRC');//设置时区
class Sqlservel{
public $server = "PC-20180326LOTY"; //服务器名称,在 sql server management studio 的登录界面查看
public $uid = null; //数据库用户名
public $pwd = null; //数据库密码
public $db = "test"; // 数据库名
public $set = "UTF-8";//字符集
public $conn = "";//数据库连接结果
private $safeCheck=true;
//构造函数
public function __construct(){
$connectionInfo = array("UID"=>$this->uid, "PWD"=>$this->pwd, "Database"=>$this->db,"CharacterSet"=>$this->set);//数据库的连接信息
$this->conn = sqlsrv_connect( $this->server, $connectionInfo);//返回true 或者 false
if($this->conn==false) {
echo "数据库连接失败<br />";
die( print_r( sqlsrv_errors(), true));
}
}
//析构函数
public function __destruct(){
$this->close();
}
//关闭打开的连接并释放与该连接关联的资源
public function close(){
if($this->conn){
sqlsrv_close($this->conn);
}
}
//准备并执行查询
public function query($sql,$params=array(),$options=array()){
if($this->safeCheck){
$result = sqlsrv_query($this->conn,$sql,$params,$options);
return $result;
}
}
//以数组形式返回一行
public function fetchRow($sql){
$res = strpos($sql,"limit");
if($res<0){
$sql.="limit 1";
}
$row = $this->query($sql);
$arr = sqlsrv_fetch_array($row);
//去除结果集中的关联数组
foreach($arr as $k=>$v){
if(is_numeric($k)){
unset($arr[$k]);
}
}
return $arr;
}
//以二维数组形式返回多条数据
public function fetchRows($sql){
$row = $this->query($sql);
while($arr = sqlsrv_fetch_array($row)){
//去除结果集中的关联数组
foreach($arr as $k=>$v){
if(is_numeric($k)){
unset($arr[$k]);
}
}
$array[] = $arr;
}
return $array;
}
//检索结果集中的行数
public function rowCount($sql){
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$result = $this->query($sql,$params,$options);
$res = sqlsrv_num_rows($result);
if ($res === false){
return "检索行计数时出错";
}else{
return $res;
}
}
}