<p>數據庫查詢不外乎4個步驟,一、創建鏈接。二、輸入查詢代碼。三、創建查詢並取出數據。四、關閉鏈接。</p> <p>php鏈接mssql數據庫有幾個注意事項,尤爲mssql的多個版本、32位、64位都有區別。</p> <p>首先,php.ini文件中;extension=php_pdo_mssql.dll ;extension=php_pdo_odbc.dll 前面的分號去掉,對應的使哪一種方式鏈接mssql。注意要重啓服務使其生效。</p> <p>1、創建鏈接</p> <p>一、odbc</p> <p>首先,在php程序所在的服務器設置odbc。這裏32位和64位操做系統有區別。32位的從控制面板中管理工具中的數據源(odbc)直接創建就能夠了,64位的要運行C:\Windows\SysWOW64\odbcad32.exe</p> <p>從這裏面設置。<font color="#ff0000">注意:上面只的是數據庫服務器爲32爲的,數據源設置服務器爲32位和64位兩種的狀況。只要兩個服務器創建的數據源位數一致就好。</font></p> <p><font color="#000000">下面是odbc創建鏈接代碼。</font></p> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" class="cnblogs_code"> <pre><span style="color: #800080">$con </span>= <span style="color: #008080">odbc_connect</span>('odbc名稱','用戶名','密碼');</pre> </div>php
<p>二、鏈接mssql2000</p>sql
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" class="cnblogs_code"> <pre><span style="color: #800080">$con</span> = mssql_connect('<span style="color: #800080">數據庫地址</span>','用戶名','密碼');</pre> </div>數據庫
<p>三、鏈接mssql2008</p>api
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" class="cnblogs_code"> <pre><span style="color: #800080">$connectionInfo</span> = <span style="color: #0000ff">array</span>("UID"=><span style="color: #800080">用戶名</span>,"PWD"=><span style="color: #800080">密碼</span>,"Database"=>"數據庫名稱"<span style="color: #000000">); </span><span style="color: #800080">$con</span> = sqlsrv_connect( <span style="color: #800080">數據庫地址</span>,<span style="color: #800080">$connectionInfo</span>);</pre> </div>服務器
<p> </p>函數
<p>2、輸入查詢代碼</p>工具
<p>這個都同樣,能夠直接寫入,也能夠從mssql中驗證好後複製過來。簡單點說就是把一個sql語句賦值給一個變量。</p>sqlserver
<p>相似下面代碼</p>fetch
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" class="cnblogs_code"> <pre><span style="color: #800080">$query</span> = "SELECT top 12 * 數據庫名稱 order by id desc";</pre> </div>spa
<p> </p>
<p>3、創建查詢並取出數據</p>
<p>一、odbc</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" class="cnblogs_code"> <pre><span style="color: #800080">$result</span> = <span style="color: #008080">odbc_do</span>(<span style="color: #800080">$con</span>,<span style="color: #800080">$query</span><span style="color: #000000">); </span><span style="color: #0000ff">while</span>(<span style="color: #008080">odbc_fetch_row</span>(<span style="color: #800080">$result</span><span style="color: #000000">)) { </span><span style="color: #800080">$變量名稱</span> = <span style="color: #008080">odbc_result</span>(<span style="color: #800080">$result</span>, "字段名稱")<span style="color: #000000">; }</span></pre> </div>
<p>二、鏈接mssql2000</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" class="cnblogs_code"> <pre><span style="color: #800080">$result</span> = mssql_query(<span style="color: #800080">$con</span>, <span style="color: #800080">$query</span><span style="color: #000000">); </span><span style="color: #0000ff">while</span>(<span style="color: #800080">$row</span> =mssql_fetch_array(<span style="color: #800080">$result</span><span style="color: #000000">)) { </span><span style="color: #800080">$變量名稱</span> = <span style="color: #800080">$row</span>["字段名稱"<span style="color: #000000">]; }</span></pre> </div>
<p>三、鏈接mssql2008</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" class="cnblogs_code"> <pre><span style="color: #800080">$result</span> = sqlsrv_query(<span style="color: #800080">$con</span>, <span style="color: #800080">$query</span><span style="color: #000000">); </span><span style="color: #0000ff">while</span>(<span style="color: #800080">$row</span> = sqlsrv_fetch_array(<span style="color: #800080">$result</span><span style="color: #000000">)) { </span><span style="color: #800080">$變量名稱 </span>= <span style="color: #800080">$row</span>["字段名稱"<span style="color: #000000">]; }</span></pre> </div>
<p>在php5.3及之後的版本中不附帶sqlsrv庫了。因此要從<a href="http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx">微軟這裏</a>下載。</p>
<p>4、關閉鏈接</p>
<p>這個沒有什麼區別,分別是odbc_close();和mssql_close()和sqlsrv_close();</p>
<p>最後體會:php鏈接mssql比鏈接mssql的函數少了一些,可是也夠用了。具體函數能夠參考<a href="http://www.php.net/mssql">php官方手冊</a>或者<a href="http://www.ostools.net/apidocs/apidoc?api=php-zh">oschina的php中文文檔</a>。</p>