php的mssql數(shù)據(jù)庫連接類實例
更新時間:2014年11月28日 11:03:36 投稿:shichen2014
這篇文章主要介紹了php的mssql數(shù)據(jù)庫連接類,以一個類實例的形式演示了PHP實現(xiàn)針對mssql數(shù)據(jù)庫的各種常用操作方法,包括對數(shù)據(jù)庫的連接與增刪改查等操作,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php的mssql數(shù)據(jù)庫連接類實例代碼,分享給大家供大家參考。
具體實現(xiàn)代碼如下:
復制代碼 代碼如下:
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.");
}
}
希望本文所述對大家的PHP程序設計有所幫助。
您可能感興趣的文章:
- PHP中數(shù)據(jù)庫單例模式的實現(xiàn)代碼分享
- PHP基于單例模式實現(xiàn)的數(shù)據(jù)庫操作基類
- PHP單例模式應用示例【多次連接數(shù)據(jù)庫只實例化一次】
- PHP實現(xiàn)HTML頁面靜態(tài)化的方法
- PHP實現(xiàn)頁面靜態(tài)化的超簡單方法
- 使用ob系列函數(shù)實現(xiàn)PHP網(wǎng)站頁面靜態(tài)化
- 詳解php實現(xiàn)頁面靜態(tài)化原理
- 利用php的ob緩存機制實現(xiàn)頁面靜態(tài)化方法
- PHP DB 數(shù)據(jù)庫連接類定義與用法示例
- PHP實現(xiàn)的sqlite數(shù)據(jù)庫連接類
- PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化實現(xiàn)方法
相關文章
php簡單實現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù)
這篇文章主要介紹了php簡單實現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù),并附上2則示例代碼,非常的簡單實用,有需要的小伙伴可以參考下。2015-04-04如何解決CI框架的Disallowed Key Characters錯誤提示
本篇文章是對解決CodeIgniter框架應用中,出現(xiàn)Disallowed Key Characters錯誤提示的方法,進行了詳細的分析介紹,需要的朋友可以參考下2013-07-07PHP目錄與文件操作技巧總結(創(chuàng)建,刪除,遍歷,讀寫,修改等)
這篇文章主要介紹了PHP目錄與文件操作技巧,結合實例形式總結分析了php針對文件與目錄的獲取、運算、打開、創(chuàng)建、讀取、寫入、修改、刪除、判斷等常見操作技巧,需要的朋友可以參考下2016-09-09php計數(shù)排序算法的實現(xiàn)代碼(附四個實例代碼)
計數(shù)排序(Counting sort)是一種根據(jù)小整數(shù)鍵對一組對象排序的算法;也就是說,它是一個整數(shù)排序算法。它通過計算具有不同鍵值的對象的數(shù)量,并對這些數(shù)量使用算術來確定輸出序列中每個鍵值的位置2020-03-03PHP統(tǒng)計數(shù)值數(shù)組中出現(xiàn)頻率最多的10個數(shù)字的方法
這篇文章主要介紹了PHP統(tǒng)計數(shù)值數(shù)組中出現(xiàn)頻率最多的10個數(shù)字的方法,涉及php中array_count_values與arsort等方法的相關使用技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04同臺服務器使用緩存APC效率高于Memcached的演示代碼
之前看到有文章說同臺服務器上APC的效率是Memcached的7倍,APC效率比Memcached高是肯定的,至于倒底快多少,我寫了個小程序測試了下。2010-02-02