溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

php中如何連接MSSQL

發(fā)布時(shí)間:2021-06-30 17:05:24 來(lái)源:億速云 閱讀:133 作者:Leah 欄目:數(shù)據(jù)庫(kù)

php中如何連接MSSQL,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

  1.連接MSSQL

  $conn=mssql_connect("實(shí)例名或者服務(wù)器IP","用戶(hù)名","密碼");

  //測(cè)試連接

  if($conn)

  {

  echo "連接成功";

  }

  2.選擇要連接的數(shù)據(jù)庫(kù)

  1mssql_select_db("dbname");

  3.執(zhí)行查詢(xún)

  $rs = mssql_query("select top 1 id,username from tbname",$conn);

  // 或者直接執(zhí)行update,insert等語(yǔ)句,可以不用為返回結(jié)果賦值

  mssql_query("update tbname set username='niunv' where id=1");

  4.獲取記錄集行數(shù)

  1echo mssql_num_rows($rs);

  5.獲取記錄集

  if($row=mssql_fetch_array($rs))

  {

  $id = $row[0];//獲取ID字段值

  $username = $row[1];//獲取username字段值

  }

  6.獲取新增記錄的ID

  將id字段設(shè)置為IDENTITY字段,執(zhí)行insert語(yǔ)句以后,就會(huì)產(chǎn)生一個(gè) @@IDENTITY 全局變量值,查詢(xún)出來(lái)就是最后一條新增記錄的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.釋放記錄集

  1mssql_free_result($rs);

  8.關(guān)閉連接

  1mssql_close($conn);

  注:用PHP操作MSSQL比在ASP連接MYSQL要簡(jiǎn)單,所以,當(dāng)需要MSSQL與MYSQL并存時(shí),用PHP連接MSSQL來(lái)操作MYSQL與MSSQL并存比較簡(jiǎn)單好用.如果是ASP連接MYSQL,需要安裝一個(gè)MYSQL驅(qū)動(dòng),默認(rèn)windows的ODBC沒(méi)有安裝,很遺憾...

  在web服務(wù)器上至少安裝了mssql的客戶(hù)端

  打開(kāi)php.ini把;extension=php_mssql.dll 前面的分號(hào)去掉

  有必要話:需要制定extension_dir

  推薦使用 php<=4.0.9 <=5.0.3目前 5.0.3="" class="" mssql="" var="" linkid="0;" pagenum="0;//分頁(yè)用---共有幾條數(shù)據(jù)" function="" server="" username="" password="" database="" this-="">server = $Server;

  $this->userName = $UserName;

  $this->passWord = $PassWord;

  $this->dataBase = $DataBase;

  }

  /**

  *數(shù)據(jù)庫(kù)連接

  **/

  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;

  }

  /**

  *數(shù)據(jù)操作

  **/

  function query($Str){

  if ($this->linkID == 0) {

  $this->ER = "數(shù)據(jù)庫(kù)還沒(méi)有連接!!";

  }

  $this->queryResult = mssql_query($Str);

  //$this->queryResult = mssql_query($Str,$this->linkID);

  if (!$this->queryResult) {

  $this->ER = "$Str.沒(méi)有操作成功,query error!!";

  return 0;//****************對(duì)于php 4.3.9以上版本的錯(cuò)誤用1

  }

  return $this->queryResult;

  }

  /**

  *數(shù)據(jù)獲取

  **/

  function fetch_array($result){

  if($result != "") $this->queryResult = $result;

  $rec =mssql_fetch_array($this->queryResult);

  if(is_array($rec)){

  return $rec;

  }

  //$this->ER = "沒(méi)有獲取數(shù)據(jù)!";

  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);

  }

  /**

  *獲取影響的的行數(shù)

  *獲取操作過(guò)的行數(shù)

  **/

  function num_rows($result=""){

  if ($result != "") {

  $this->queryResult = $result;

  $row = mssql_num_rows($this->queryResult);

  return $row;

  }

  }

  /**

  *獲取查詢(xún)結(jié)果---多個(gè)

  **/

  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;

  }

  /**

  *數(shù)據(jù)庫(kù)信息分頁(yè)

  *$Result 數(shù)據(jù)庫(kù)操作

  *str ==sql語(yǔ)句

  *page ==第幾頁(yè)

  *showNum ==顯示幾頁(yè)

  */

  function page($Str,$Page=0,$ShowNum=5){

  $back = array();//返回?cái)?shù)據(jù)

  $maxNum = 0;

  if ($Str == "") {

  $this->ER = "沒(méi)有數(shù)據(jù)";

  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){ 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;

  }

  /**

  *分頁(yè)的html頁(yè)碼

  */

  function page_html($DataNum=0,$Page=1,$ShowNum=3,$web,$Post=''){

  if ($DataNum == 0) {

  $back = "沒(méi)有要查詢(xún)的數(shù)據(jù)";

  }else {

  if ($ShowNum<=0) {   $ShowNum = 3;   }   if ($Page<=0) {   $Page = 1;   }   if (empty($web)) {   $web = "#";   }   $pageNum = ceil($DataNum/$ShowNum);   if ($Page <= 1) {   $top = "首頁(yè)<<";   }else {   $top = "首頁(yè)<< ";   }   if ($Page !==1) {   $upPage = "上一頁(yè)";   }else {   $upPage = "上一頁(yè)";   }   if ($Page < $pageNum) {   $downPage = "下一頁(yè)";   }else {   $downPage = "下一頁(yè)";   }   if ($Page == $pageNum) {   $foot = ">>尾頁(yè)";

  }else {

  $foot = " >>尾頁(yè)";

  }

  $back = <<   共 $pageNum 頁(yè)   第 $Page/$pageNum 頁(yè) $top $upPage $downPage $foot   EOT;   }   return $back;   }   }//end class   ?>

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI