php的mssql數(shù)據(jù)庫(kù)連接類實(shí)例
本文實(shí)例講述了php的mssql數(shù)據(jù)庫(kù)連接類實(shí)例代碼,分享給大家供大家參考。
具體實(shí)現(xiàn)代碼如下:
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ǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)返回json數(shù)據(jù)
這篇文章主要介紹了php簡(jiǎn)單實(shí)現(xiàn)查詢數(shù)據(jù)庫(kù)返回json數(shù)據(jù),并附上2則示例代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-04-04使用PHP獲取當(dāng)前url路徑的函數(shù)以及服務(wù)器變量
本篇文章是對(duì)使用PHP獲取當(dāng)前url路徑的函數(shù)以及服務(wù)器變量的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP實(shí)現(xiàn)文件下載【實(shí)例分享】
本篇文章主要介紹了PHP實(shí)現(xiàn)文件下載的示例代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04如何解決CI框架的Disallowed Key Characters錯(cuò)誤提示
本篇文章是對(duì)解決CodeIgniter框架應(yīng)用中,出現(xiàn)Disallowed Key Characters錯(cuò)誤提示的方法,進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下2013-07-07PHP目錄與文件操作技巧總結(jié)(創(chuàng)建,刪除,遍歷,讀寫,修改等)
這篇文章主要介紹了PHP目錄與文件操作技巧,結(jié)合實(shí)例形式總結(jié)分析了php針對(duì)文件與目錄的獲取、運(yùn)算、打開、創(chuàng)建、讀取、寫入、修改、刪除、判斷等常見操作技巧,需要的朋友可以參考下2016-09-09php計(jì)數(shù)排序算法的實(shí)現(xiàn)代碼(附四個(gè)實(shí)例代碼)
計(jì)數(shù)排序(Counting sort)是一種根據(jù)小整數(shù)鍵對(duì)一組對(duì)象排序的算法;也就是說(shuō),它是一個(gè)整數(shù)排序算法。它通過(guò)計(jì)算具有不同鍵值的對(duì)象的數(shù)量,并對(duì)這些數(shù)量使用算術(shù)來(lái)確定輸出序列中每個(gè)鍵值的位置2020-03-03PHP統(tǒng)計(jì)數(shù)值數(shù)組中出現(xiàn)頻率最多的10個(gè)數(shù)字的方法
這篇文章主要介紹了PHP統(tǒng)計(jì)數(shù)值數(shù)組中出現(xiàn)頻率最多的10個(gè)數(shù)字的方法,涉及php中array_count_values與arsort等方法的相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04同臺(tái)服務(wù)器使用緩存APC效率高于Memcached的演示代碼
之前看到有文章說(shuō)同臺(tái)服務(wù)器上APC的效率是Memcached的7倍,APC效率比Memcached高是肯定的,至于倒底快多少,我寫了個(gè)小程序測(cè)試了下。2010-02-02