php串連mssql的一些相關經驗和總結_PHP教程

來源:互聯網
上載者:User
為了能讓PHP串連MSSQL,系統需要安裝MSSQL,PHP,且在PHP.ini中的配置中,將 ;extension=php_mssql.dll前面的;去掉 1.串連MSSQL $conn=mssql_connect("執行個體名或者伺服器IP","使用者名稱","密碼"); //測試連接 if($conn) { echo "串連成功"; } 2.選擇要串連的資料庫 mssql_select_db("dbname"); 3.執行查詢 $rs = mssql_query("select top 1 id,username from tbname",$conn); 或者直接執行update,insert等語句,可以不用為返回結果賦值 mssql_query("update tbname set username='niunv' where id=1"); 4.擷取記錄集行數 echo mssql_num_rows($rs); 5.擷取記錄集 if($row=mssql_fetch_array($rs)) { $id = $row[0];//擷取ID欄位值 $username = $row[1];//擷取username欄位值 } 6.擷取新增記錄的ID 將id欄位設定為IDENTITY欄位,執行insert語句以後,就會產生一個 @@IDENTITY 全域變數值,查詢出來就是最後一條新增記錄的ID了. mssql_query("insert into tbname(username) values ('nv')",$conn); $rs = mssql_query("select @@IDENTITY as id",$conn); if($row=mssql_fetch_array($rs)) { echo $row[0]; } 7.釋放記錄集 //更多 http://www.52mvc.com mssql_free_result($rs); 8.關閉串連 mssql_close($conn); 注:用PHP操作MSSQL比在ASP串連MYSQL要簡單,所以,當需要MSSQL與MYSQL並存時,用PHP串連MSSQL來操作MYSQL與MSSQL並存比較簡單好用.如果是ASP串連MYSQL,需要安裝一個MYSQL驅動,預設windows的ODBC沒有安裝,很遺憾... 1.在web伺服器上至少安裝了mssql的用戶端 2.開啟php.ini把;extension=php_mssql.dll 前面的分號去掉 有必要話:需要制定extension_dir 3.推薦使用 php<=4.0.9 <=5.0.3目前 我還沒有串連成功過4.010和 5.0.3 4.資料庫的 串連分頁可以 到 phpe.net上擷取到相應的class 下面是我根據那裡 修改的 一個class server = $Server; $this->userName = $UserName; $this->passWord = $PassWord; $this->dataBase = $DataBase; } /** *資料庫連接 **/ function db_connect(){ $this->linkID = mssql_pconnect($this->server,$this->userName,$this->passWord); if(!$this->linkID){ $this->ER = "db_connect($this->server,$this->userName,$this->passWord) error"; return 0; } if (!mssql_select_db($this->dataBase,$this->linkID)) { $this->ER = "mssql_select_db($this->dataBase,$this->lastInsertID) error"; return 0; } return $this->linkID; } /**public * function: Check the database, if exist then select * exist: return 1 * not exist: return 0 */ function selectDatabase(){ if(mssql_select_db($this->dataBase)) return 1; else return 0; } /** *資料操作 **/ function query($Str){ if ($this->linkID == 0) { $this->ER = "資料庫還沒有串連!!"; } $this->queryResult = mssql_query($Str); //$this->queryResult = mssql_query($Str,$this->linkID); if (!$this->queryResult) { $this->ER = "$Str.沒有操作成功,query error!!"; return 0;//****************對於php 4.3.9以上版本的錯誤用1 } return $this->queryResult; } /** *資料擷取 **/ function fetch_array($result){ if($result != "") $this->queryResult = $result; $rec =mssql_fetch_array($this->queryResult); if(is_array($rec)){ return $rec; } //$this->ER = "沒有擷取資料!"; return 0; } /**public * function: Free the Query Result * success return 1 * failed: return 0 */ function freeResult($result=""){ if($result != "") $this->queryResult = $result; return mssql_free_result($this->queryResult); } /** *擷取影響的的行數 *擷取操作過的行數 **/ function num_rows($result=""){ if ($result != "") { $this->queryResult = $result; $row = mssql_num_rows($this->queryResult); return $row; } } /** *擷取查詢結果---多個 **/ function result_ar($str=''){ if (empty($str)) { return 0; } $back = array(); $this->queryResult = $this->query($str); while ($row = $this->fetch_array($this->queryResult)) { $back[] = $row; } return $back; } /** *資料庫資訊分頁 *$Result 資料庫操作 *str ==sql語句 *page ==第幾頁 *showNum ==顯示幾頁 */ function page($Str,$Page=0,$ShowNum=5){ $back = array();//返回資料 $maxNum = 0; if ($Str == "") { $this->ER = "沒有資料"; return 0; } $this->queryResult = $this->query($Str); if($this->queryResult){ if($Page==""){ $nopa=0; }else{ $nopa = ($Page-1)*$ShowNum; if ($nopa<0) { $nopa = 0; } } $maxNum=$this->num_rows($this->queryResult); $k=0; $i=0; $dd=$this->fetch_array($this->queryResult); while($dd&&$nopa<=$maxNum&&$i<$ShowNum){ if($nopa >= $maxNum) $nopa = $maxNum; mssql_data_seek($this->queryResult,$nopa); $row=$this->fetch_array($this->queryResult); $nopa++; $i++; $back[] = $row; if ($nopa >=$maxNum) { break; } } } $this->pageNum = $maxNum; return $back; } /** *分頁的html頁碼 */ function page_html($DataNum=0,$Page=1,$ShowNum=3,$web,$Post=''){ if ($DataNum == 0) { $back = "沒有要查詢的資料"; }else { if ($ShowNum<=0) { $ShowNum = 3; } if ($Page<=0) { $Page = 1; } if (empty($web)) { $web = "#"; } $pageNum = ceil($DataNum/$ShowNum); if ($Page <= 1) { $top = "首頁<<"; }else { $top = "首頁<< "; } if ($Page !==1) { $upPage = "上一頁"; }else { $upPage = "上一頁"; } if ($Page < $pageNum) { $downPage = "下一頁"; }else { $downPage = "下一頁"; } if ($Page == $pageNum) { $foot = ">>尾頁"; }else { $foot = " >>尾頁"; } $back = <<

http://www.bkjia.com/PHPjc/477777.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477777.htmlTechArticle為了能讓PHP串連MSSQL,系統需要安裝MSSQL,PHP,且在PHP.ini中的配置中,將 ;extension=php_mssql.dll前面的;去掉 1.串連MSSQL $conn=mssql_connect(執行個體名或者服務...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.