这篇文章主要为大家详细介绍了php 完全面向对象操作mssql数据库类的入门实例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
<?php
/**
* 操作mssql数据库类
*
* @param
* @arrange (512.笔记) www.q1010.com
**/
class database_mssql {
var $database = NULL;
var $sqls = NULL;
var $host = NULL;
var $username = NULL;
var $password = NULL;
var $databaseName = NULL;
var $link = NULL;
var $queries = NULL;
var $errors = NULL;
function database_mssql($host, $username, $password, $database) {
$this->host = $host;
$this->username = sha1($username);
$this->password = sha1($password);
$this->database = $database;
$this->link = "";
$this->queries = array ();
$this->errors = array ();
$this->sqls = array ();
$this->link = mssql_connect($this->host, $username, $password);
mssql_select_db($this->database, $this->link);
}
function justquery($sql) {
$this->queries[] = $sql;
return mssql_query($sql, $this->link);
}
function loadResult($sql) {
if (!($cur = $this->justquery($sql))) {
return null;
}
$ret = null;
if ($row = mssql_fetch_row( $cur )) {
$ret = $row[0];
}
mssql_free_result( $cur );
return $ret;
}
function loadFirstRow($sql) {
if (!($cur = $this->justquery($sql))) {
return null;
}
$ret = null;
if ($row = mssql_fetch_object( $cur )) {
$ret = $row;
}
mssql_free_result( $cur );
return $ret;
}
function insertid() {
//return mysql_insert_id( $this->link );
}
function query($sql, $key = "", $returns = true, $batch = false) {
$sqls = $result = array ();
switch ($batch) {
default:
case true:
foreach ($sql as $index => $query) {
$this->queries[] = $query;
$answer = mssql_query($query, $this->link);
if (!$answer) {
$this->errors[] = "n/a";//odbc_errormsg($this->link);
}
else {
if ($returns != false) {
if (mssql_num_rows($answer) > 0){
while ($row = mssql_fetch_object($answer)) {
if ($key != ""){
$result[$index][$row->$key] = $row;
}
else {
$result[$index][] = $row;
}
}
} else {}
} else {}
}
}
break;
case false:
$this->queries[] = $sql;
$answer = mssql_query($sql, $this->link);
if (!$answer) {
$this->errors[] = "n/a";//odbc_errormsg($this->link);
$result = false;
}
else {
if ($returns != false) {
if (mssql_num_rows($answer) > 0){
while ($row = mssql_fetch_object($answer)) {
if ($key != ""){
$result[$row->$key] = $row;
}
else {
$result[] = $row;
}
}
} else {}
}
else {
$result = true;
}
}
break;
}
return $result;
}
function loadObject( $sql, &$object ) {
if ($object != null) {
if (!($cur = $this->justquery($sql))) {
return false;
} else {}
if ($array = mssql_fetch_array( $cur )) {
mssql_free_result( $cur );
$this->bindArrayToObject( $array, $object);
return true;
}
else {
return false;
}
}
else {
if ($cur = $this->justquery($sql)) {
if ($object = mssql_fetch_object( $cur )) {
mssql_free_result( $cur );
return true;
}
else {
$object = null;
return false;
}
}
else {
return false;
}
}
}
function bindArrayToObject( $array, &$obj) {
if (!is_array( $array ) || !is_object( $obj )) {
return (false);
}
foreach (get_object_vars($obj) as $k => $v) {
if( substr( $k, 0, 1 ) != '_' ) {
$ak = $k;
if (isset($array[$ak])) {
$obj->$k = $array[$ak];
}
}
}
return true;
}
function formatCSVCell($data) {
$useQuotes = false;
$quotable = array (
""" => """",
"," => ",",
"
" => "
"
);
foreach ($quotable as $char => $repl) {
if (eregi($char, $data)) {
$useQuotes = true;
} else {}
}
if ($useQuotes == true) {
foreach ($quotable as $char => $repl) {
$data = str_replace($char, $repl, $data);
}
$data = """ . $data . """;
}
else {
}
return $data;
}
}
/*** 来自:四海网(www.q1010.com) ***/
?>
本文来自:http://www.q1010.com/173/559-0.html
注:关于php 完全面向对象操作mssql数据库类的入门实例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:操作数据库
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。