php的mssql數(shù)據(jù)庫(kù)連接類實(shí)例
更新時(shí)間:2014年11月28日 11:03:36 投稿:shichen2014
這篇文章主要介紹了php的mssql數(shù)據(jù)庫(kù)連接類,以一個(gè)類實(shí)例的形式演示了PHP實(shí)現(xiàn)針對(duì)mssql數(shù)據(jù)庫(kù)的各種常用操作方法,包括對(duì)數(shù)據(jù)庫(kù)的連接與增刪改查等操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了php的mssql數(shù)據(jù)庫(kù)連接類實(shí)例代碼,分享給大家供大家參考。
具體實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
class DB_Sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row = 0;
var $Errno = 0;
var $Error = "";
var $Auto_Free = 0; ## set this to 1 to automatically free results
function DB_Sql($query = "") {
$this->query($query);
}
function connect() {
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mssql_connect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID)
$this->halt("Link-ID == false, mssql_pconnect failed");
else
@mssql_select_db($this->Database, $this->Link_ID);
}
}
function free_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
function query($Query_String)
{
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
if (!$this->Link_ID)
$this->connect();
# printf("<br>Debug: query = %s<br> ", $Query_String);
$this->Query_ID = mssql_query($Query_String, $this->Link_ID);
$this->Row = 0;
if (!$this->Query_ID) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Invalid SQL: ".$Query_String);
}
return $this->Query_ID;
}
function next_record() {
if ($this->Record = mssql_fetch_row($this->Query_ID)) {
// add to Record[<key>]
$count = mssql_num_fields($this->Query_ID);
for ($i=0; $i<$count; $i++){
$fieldinfo = mssql_fetch_field($this->Query_ID,$i);
$this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
}
$this->Row += 1;
$stat = 1;
} else {
if ($this->Auto_Free) {
$this->free_result();
}
$stat = 0;
}
return $stat;
}
function seek($pos) {
mssql_data_seek($this->Query_ID,$pos);
$this->Row = $pos;
}
function metadata($table) {
$count = 0;
$id = 0;
$res = array();
$this->connect();
$id = mssql_query("select * from $table", $this->Link_ID);
if (!$id) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Metadata query failed.");
}
$count = mssql_num_fields($id);
for ($i=0; $i<$count; $i++) {
$info = mssql_fetch_field($id, $i);
$res[$i]["table"] = $table;
$res[$i]["name"] = $info["name"];
$res[$i]["len"] = $info["max_length"];
$res[$i]["flags"] = $info["numeric"];
}
$this->free_result();
return $res;
}
function affected_rows() {
// Not a supported function in PHP3/4. Chris Johnson, 16May2001.
// return mssql_affected_rows($this->Query_ID);
$rsRows = mssql_query("Select @@rowcount as rows", $this->Link_ID);
if ($rsRows) {
return mssql_result($rsRows, 0, "rows");
}
}
function num_rows() {
return mssql_num_rows($this->Query_ID);
}
function num_fields() {
return mssql_num_fields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Field_Name) {
return $this->Record[strtolower($Field_Name)];
}
function p($Field_Name) {
print $this->f($Field_Name);
}
function halt($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br> ", $msg);
printf("<b>MSSQL Error</b>: %s (%s)<br> ",
$this->Errno,
$this->Error);
die("Session halted.");
}
}
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row = 0;
var $Errno = 0;
var $Error = "";
var $Auto_Free = 0; ## set this to 1 to automatically free results
function DB_Sql($query = "") {
$this->query($query);
}
function connect() {
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mssql_connect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID)
$this->halt("Link-ID == false, mssql_pconnect failed");
else
@mssql_select_db($this->Database, $this->Link_ID);
}
}
function free_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
function query($Query_String)
{
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
if (!$this->Link_ID)
$this->connect();
# printf("<br>Debug: query = %s<br> ", $Query_String);
$this->Query_ID = mssql_query($Query_String, $this->Link_ID);
$this->Row = 0;
if (!$this->Query_ID) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Invalid SQL: ".$Query_String);
}
return $this->Query_ID;
}
function next_record() {
if ($this->Record = mssql_fetch_row($this->Query_ID)) {
// add to Record[<key>]
$count = mssql_num_fields($this->Query_ID);
for ($i=0; $i<$count; $i++){
$fieldinfo = mssql_fetch_field($this->Query_ID,$i);
$this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
}
$this->Row += 1;
$stat = 1;
} else {
if ($this->Auto_Free) {
$this->free_result();
}
$stat = 0;
}
return $stat;
}
function seek($pos) {
mssql_data_seek($this->Query_ID,$pos);
$this->Row = $pos;
}
function metadata($table) {
$count = 0;
$id = 0;
$res = array();
$this->connect();
$id = mssql_query("select * from $table", $this->Link_ID);
if (!$id) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Metadata query failed.");
}
$count = mssql_num_fields($id);
for ($i=0; $i<$count; $i++) {
$info = mssql_fetch_field($id, $i);
$res[$i]["table"] = $table;
$res[$i]["name"] = $info["name"];
$res[$i]["len"] = $info["max_length"];
$res[$i]["flags"] = $info["numeric"];
}
$this->free_result();
return $res;
}
function affected_rows() {
// Not a supported function in PHP3/4. Chris Johnson, 16May2001.
// return mssql_affected_rows($this->Query_ID);
$rsRows = mssql_query("Select @@rowcount as rows", $this->Link_ID);
if ($rsRows) {
return mssql_result($rsRows, 0, "rows");
}
}
function num_rows() {
return mssql_num_rows($this->Query_ID);
}
function num_fields() {
return mssql_num_fields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Field_Name) {
return $this->Record[strtolower($Field_Name)];
}
function p($Field_Name) {
print $this->f($Field_Name);
}
function halt($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br> ", $msg);
printf("<b>MSSQL Error</b>: %s (%s)<br> ",
$this->Errno,
$this->Error);
die("Session halted.");
}
}
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- PHP中數(shù)據(jù)庫(kù)單例模式的實(shí)現(xiàn)代碼分享
- PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫(kù)操作基類
- PHP單例模式應(yīng)用示例【多次連接數(shù)據(jù)庫(kù)只實(shí)例化一次】
- PHP實(shí)現(xiàn)HTML頁(yè)面靜態(tài)化的方法
- PHP實(shí)現(xiàn)頁(yè)面靜態(tài)化的超簡(jiǎn)單方法
- 使用ob系列函數(shù)實(shí)現(xiàn)PHP網(wǎng)站頁(yè)面靜態(tài)化
- 詳解php實(shí)現(xiàn)頁(yè)面靜態(tài)化原理
- 利用php的ob緩存機(jī)制實(shí)現(xiàn)頁(yè)面靜態(tài)化方法
- PHP DB 數(shù)據(jù)庫(kù)連接類定義與用法示例
- PHP實(shí)現(xiàn)的sqlite數(shù)據(jù)庫(kù)連接類
- PHP單例模式數(shù)據(jù)庫(kù)連接類與頁(yè)面靜態(tài)化實(shí)現(xiàn)方法
相關(guān)文章
PHP結(jié)合Ffmpeg快速搭建流媒體服務(wù)的實(shí)踐記錄
這篇文章主要給大家介紹了關(guān)于使用PHP結(jié)合Ffmpeg快速搭建流媒體服務(wù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
PHP后期靜態(tài)綁定之self::限制實(shí)例分析
這篇文章主要介紹了PHP后期靜態(tài)綁定之self::限制,結(jié)合實(shí)例形式分析了php后期靜態(tài)綁定self::與static相關(guān)使用技巧,需要的朋友可以參考下2018-12-12
PHP中怎樣保持SESSION不過(guò)期 原理及方案介紹
本文主要討論WEB SESSION,其一般有兩種:客戶端SESSION和服務(wù)器端SESSION,后一種最常見(jiàn)的屬于Java Beans提供的2013-08-08
PHP中simplexml_load_string函數(shù)使用說(shuō)明
這個(gè)問(wèn)題遇到好幾次了,今天翻看以前代碼的時(shí)候看到,便記下來(lái),需要的朋友可以參考下。2011-01-01
php網(wǎng)站被掛木馬后的修復(fù)方法總結(jié)
這篇文章主要介紹了php網(wǎng)站被掛木馬后的修復(fù)方法,總結(jié)分析了Linux與Windows系統(tǒng)上木馬的查殺方法,并給出了一個(gè)完整的木馬查找工具實(shí)例,需要的朋友可以參考下2014-11-11

