|
通過PHP操作SQL Server數(shù)據(jù)庫(kù) 1、連接SQL Server數(shù)據(jù)庫(kù) 雖然PHP+MySQL進(jìn)行網(wǎng)絡(luò)開發(fā)是最佳的選擇,但是對(duì)于已經(jīng)熟悉并掌握了SQL Server的讀者來說,可能會(huì)使用SQL Server作為網(wǎng)站后臺(tái)數(shù)據(jù)庫(kù),這時(shí),就需要通過使用PHP來連接SQL Server數(shù)據(jù)庫(kù)。 PHP同SQL Server數(shù)據(jù)庫(kù)的連接步驟同MySQL基本類似,而且函數(shù)結(jié)構(gòu)及聲明也基本類似,如MySQL連接服務(wù)器的函數(shù)是mysql_connect(),而連接SQL Server的函數(shù)是mssql_connect(),只要將mysql替換成mssql即可。 在默認(rèn)情況下,是不能連接SQL Server數(shù)據(jù)庫(kù)的。為了能夠連接SQL Server數(shù)據(jù)庫(kù),必須修改php.ini文件的設(shè)置(在更改文件前需要去除該文件的只讀屬性),將“extension=php_mssql.dll”語句前面的分號(hào)刪除,如圖9.18所示,然后,重新啟動(dòng)Apache服務(wù)器。 mssql_connect()函數(shù)用于連接SQL Server服務(wù)器,該函數(shù)的語法如下: resource mssql_connect([string servername[,string username[,string password]]]); 其中,參數(shù)servername表示服務(wù)器名稱;username表示用戶名;password表示密碼。 例如,用戶連接本機(jī)SQL Server服務(wù)器的代碼如下: $link=mssql_connect("localhost","sa",""); 2、圖書信息查詢 PHP操縱SQL server數(shù)據(jù)庫(kù)用到了MySQL函數(shù)庫(kù)中的相關(guān)函數(shù),下面對(duì)這些函數(shù)進(jìn)行介紹。 1)、mssql_connect()函數(shù) 該函數(shù)用于建立數(shù)據(jù)庫(kù)的連接。語法如下: resource mssql_connect(string [servername], string [username], string [password]); 2)、mssql_select_db()函數(shù) 該函數(shù)用于選擇數(shù)據(jù)庫(kù)文件。語法如下: resource mssql_select_db(string database_name, int link_identifier]); 3)、mssql_query()函數(shù) 該函數(shù)用于查詢字符串。語法如下: resource mssql_query(string query, int plink_identifier)); 4)、mssql_fetch_array()函數(shù) 該函數(shù)用于將查詢結(jié)果result分割到數(shù)組變量中。語法如下: resource array mssql_fetch_array(int result); 5)、mssql_close()函數(shù) 該函數(shù)用于關(guān)閉與數(shù)據(jù)庫(kù)的連接。語法如下: boolean mssql_close(int [link_identifier]); 信息查詢?cè)趧?dòng)態(tài)網(wǎng)站開發(fā)過程中應(yīng)用最為廣泛。下面這個(gè)范例主要介紹PHP與SQL Server進(jìn)行數(shù)據(jù)庫(kù)的操作。 本范例通過接收文本框傳遞的值賦值給session變量,來檢索與之匹配的圖書信息(支持模糊查詢),單擊【查詢】按鈕,即可將符合圖書名稱的圖書信息顯示在瀏覽器中,運(yùn)行結(jié)果如圖所示。  圖書信息查詢 本范例的實(shí)現(xiàn)過程如下。 (1)首先,利用包含文件命令include引用數(shù)據(jù)庫(kù)配置文件“conn.php”,代碼如下: <?php include "conn.php"; ?> 在“conn.php”文件中,主要應(yīng)用mssql_connect()函數(shù)和mssql_select_db()函數(shù)建立數(shù)據(jù)源的連接,代碼如下: <?php $conn=mssql_connect("localhost","sa",""); mssql_select_db("DB_Book",$conn); ?> (2)創(chuàng)建記錄集。首先,通過表單判斷用戶是否單擊了【查詢】按鈕,然后,通過接收文本框傳遞的值賦值給session變量,再利用查詢語句檢索符合條件的數(shù)據(jù)信息。最后,利用if條件語句判斷記錄集是否為空,如果為空,輸出提示信息“對(duì)不起,您檢索的圖書信息不存在!”,否則,利用Do?while循環(huán)語句將符合條件的信息輸出到瀏覽器中,程序完整代碼如下: <?php if ($_POST["Submit"]!=""){ session_start(); $_SESSION[txt_book]=$_POST[txt_book]; ?> <table width="92%" border="1"> <tr bgcolor="#DEEFF7"> <td>編號(hào)</td><td>圖書名稱</td><td>發(fā)行日期</td><td>單價(jià)</td><td width="100">內(nèi)容介紹</td> <td>作者</td><td>出版社</td></tr> <?php $sql=mssql_query("select * from tab_book where bookname like '%$_SESSION[tx t_book]%'"); $info=mssql_fetch_array($sql); if($info==False){ echo "對(duì)不起,您檢索的圖書信息不存在!"; } else{ do{ ?> <tr> <td> <?php echo $info[id];?> </td> <td width="180"> <?php echo $info[bookname];?> </td> <td> <?php echo $info[issueDate];?> </td> <td> <?php echo $info[price];?></td> <td width="260"> <?php echo $info[Synopsis];?></td> <td> <?php echo $info[Maker];?></td> <td> <?php echo $info[Pulisher];?></td> </tr> <?php } while($info=mssql_fetch_array($sql)); mssql_close(); //關(guān)閉數(shù)據(jù)庫(kù)連接 } } ?> </table> 3、 圖片分欄顯示 在圖片信息較多的頁面中,將圖片進(jìn)行分欄顯示可以使整個(gè)頁面布局清晰,下面結(jié)合具體范例介紹圖片分欄的技術(shù)。 在開發(fā)信息資源網(wǎng)站時(shí),對(duì)資源信息的顯示方法有多種,如分頁顯示、跳轉(zhuǎn)頁碼顯示等,本范例主要介紹分欄顯示信息的方法,運(yùn)行結(jié)果如圖所示。  圖片分欄顯示 首先建立數(shù)據(jù)庫(kù)連接文件,該文件代碼如下: <!--****************************conn/conn.php***************************** ******--> <?php $conn=mssql_connect("localhost","sa",""); //連接數(shù)據(jù)庫(kù)服務(wù)器 mssql_select_db("DB_mrbook",$conn); //選擇數(shù)據(jù)庫(kù)"DB_mrbook" ?> 下面開始具體實(shí)現(xiàn)商品的分欄顯示: <!--*********************************index.php***********************************--> <? php include "conn/conn.php"; //包含數(shù)據(jù)庫(kù)連接文件 ?> …… <?php $sql=mssql_query("select * from tb_writer where writer_type='平面設(shè)計(jì)' order by wr iter_data asc",$conn); //從數(shù)據(jù)庫(kù)中查詢圖片信息 ?> …… <?php $i=1; //用變量$i控制圖片分欄 while($result=mssql_fetch_array($sql)) //通過循環(huán)遍歷所有記錄 { if ($i % 5==0) //如果$i能被5整除,則顯示下一欄圖片 { ?> <td height="25"> <div> <img src="<?php echo "./writer/". $result [writer_img];?>" width="76" height="110" border="0">&nb sp;</div> </td> </tr> <?php } else //否則顯示本欄圖片 { ?> <td height="25"><div> <img s rc="<?php echo "./writer/". $result[writer_img];?>" width="76" height="110" border ="0"> </div> </td> <?php } $i++; //顯示一幅圖片后使$i加1 } ?> …… 4、 留言板 PHP操縱SQL Server,除了可以利用PHP自身的函數(shù)外,還可以利用ADO的方法操縱SQL Server數(shù)據(jù)庫(kù),下面將以留言板為例介紹PHP利用ADO的方法操縱SQL server數(shù)據(jù)庫(kù)的實(shí)現(xiàn)過程。 在開發(fā)網(wǎng)站程序設(shè)計(jì)時(shí),有很多的網(wǎng)站里都設(shè)有留言板模塊。該模塊主要用于方便用戶在網(wǎng)站內(nèi)進(jìn)行留言。留言板模塊包括查看留言、簽寫留言、管理留言。運(yùn)行本范例,用戶單擊“查看留言”超級(jí)鏈接,瀏覽留言信息,運(yùn)行結(jié)果如圖所示。  留言板頁面的運(yùn)行結(jié)果 為了減少代碼重用率,同樣建立conn.php來連接SQL server數(shù)據(jù)庫(kù),代碼如下: <!--******************************conn.php**************************************--> <?php $conn=new com("adodb.connection"); //創(chuàng)建數(shù)據(jù)庫(kù)連接對(duì)象 $connstr="provider=sqloledb;data source=localhost;uid=sa;pwd=;database=guestbook"; //設(shè)置數(shù)據(jù)庫(kù)連接驅(qū)動(dòng) $conn->open($connstr); //調(diào)用數(shù)據(jù)庫(kù)連接對(duì)象的open()方法來執(zhí)行驅(qū)動(dòng),從而與數(shù)據(jù)庫(kù)建立連接 ?> 上述代碼中的data source的值為數(shù)據(jù)庫(kù)服務(wù)器的名稱,uid的值為某用戶名,pwd的值為用戶密碼,database的值為某數(shù)據(jù)庫(kù)名。有了conn.php為基礎(chǔ),就可以通過分頁來顯示所有留言信息了。代碼如下: <!--******************************index.php**************************************--> <?php include_once("top.php"); //包含首部導(dǎo)航欄 ?> …… <?php include_once("conn.php"); //包含數(shù)據(jù)庫(kù)連接文件 $rs=new com("adodb.recordset"); //建立記錄集對(duì)象 $sql="select * from word order by shijian desc"; $rs->open($sql,$conn,3,1); //執(zhí)行查詢 if($rs->eof || $rs->bof) //如果記錄集指針的位置在第一行以前或在最后一行以后,此時(shí)說明記錄集中無內(nèi)容 { ?> <tr> <td height="20" colspan="5" >暫無留言</td> </tr> <?php } else { if($_GET[page]=="" || is_numeric($_GET[page]==false)) { $page=1; //獲取查詢字符串的值,為下面翻頁所用 } else { $page=intval($_GET[page]); } $rs->pagesize=5; //設(shè)置每頁顯示5條留言 $rs->absolutepage=$page; //設(shè)置當(dāng)前顯示第幾頁 $mypagesize=$rs->pagesize; //用$mypagesize來控制每頁循環(huán)顯示 while(!$rs->eof && $mypagesize>0) { ?> …… 本站共有留言<?php echo $rs->recordcount;?>條 每頁顯示<?php echo $rs->pagesize;?>條 第<?php echo $page;?>頁/共<?php echo $rs->pagecount;?>頁 <?php if($page>=2) //下面代碼用于實(shí)現(xiàn)翻頁顯示 { ?> <a href="index.php?page=1" title="首頁"><font face="webdings"> 9 </font></a> <a href="index.php?page=<?php echo $page-1;?>" title="前一頁"><font face="webdings"> 7 </font></a> <?php } if($rs->pagecount<=4) { for($i=1;$i<=$rs->pagecount;$i++) { ?> <a href="index.php?page=<?php echo $i;?>"><?php echo $i;?></a> <?php } } else { for($i=1;$i<=4;$i++) { ?> <a href="index.php?page=<?php echo $i;?>"><?php echo $i;?></a> <?php } ?> <a href="index.php?page=<?php if($rs->pagecount>=$page+1) echo $page+1; else echo 1; ?>" title="后一頁"><font face="webdings"> 8 </font></a> <a href="index.php?page=<?php echo $rs->pagecount;?>" title="尾頁"><font face="webdings"> : </font></a> <?php } ?> </div></td> </tr> </table> <?php include_once("bottom.php"); 通過以上代碼,用戶可以掌握PHP利用ADO的方式操縱SQL Server數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,該留言板的其他功能及詳細(xì)代碼請(qǐng)參看本書附帶光盤。
信息發(fā)布:廣州名易軟件有限公司 http://m.jetlc.com
|