PHP調(diào)用三種數(shù)據(jù)庫(kù)的方法

冠縣ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!
本文比較詳細(xì)的介紹PHP調(diào)用MySQL、ODBC以及ORACLE數(shù)據(jù)庫(kù)。
MySQL是一個(gè)小巧靈瓏的數(shù)據(jù)庫(kù)服務(wù)器軟件,對(duì)于中、小型應(yīng)用系統(tǒng)是非常理想的。除了支持標(biāo)準(zhǔn)的ANSI SQL語(yǔ)句外,最重要的是,它還支持多種平臺(tái),而在Unix/Linux系統(tǒng)上,MySQL支持多線程運(yùn)行方式,從而能獲得相當(dāng)好的性能。它和PHP、 Apache一樣,是屬于開(kāi)放源代碼軟件。其官方網(wǎng)站是:,上面提供Windows,Linux,Unix版本的源代碼的下載。
注意,MySQL訪問(wèn)函數(shù)都需要有相應(yīng)的權(quán)限才能運(yùn)行。常用的相關(guān)函數(shù)介紹如下:
(1)integer mysql_connect(主機(jī),用戶(hù)名,口令);
此函數(shù)開(kāi)始一個(gè)對(duì)指定主機(jī)上的MySQL數(shù)據(jù)庫(kù)的連接。若該數(shù)據(jù)庫(kù)位于一個(gè)不同地端口,則在主機(jī)名后加上冒號(hào)和端口號(hào)。所有參數(shù)均為可選的,缺省情況下分別對(duì)應(yīng)為本地主機(jī)、用戶(hù)正在執(zhí)行的腳本名和空。主機(jī)可以是IP地址或域名。
在腳本執(zhí)行結(jié)束時(shí),連接被自動(dòng)關(guān)閉,也可以用mysql_close提前關(guān)閉。
(2)boolean mysql_create_db(數(shù)據(jù)庫(kù)名);
創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)。注意必須用一個(gè)帶有創(chuàng)建數(shù)據(jù)庫(kù)許可權(quán)的帳號(hào)打開(kāi)連接。
(3)boolean mysql_select_db(數(shù)據(jù)庫(kù)名,連接號(hào));
選擇缺省數(shù)據(jù)庫(kù)。
(4)integer mysql_query(SQL語(yǔ)句,連接號(hào));
對(duì)指定數(shù)據(jù)庫(kù)進(jìn)行查詢(xún)。如果SQL語(yǔ)句是select,則返回一個(gè)結(jié)果號(hào),否則返回的值可以不理會(huì)。如果失敗,返回false.。
(5)array mysql_fetch_array(結(jié)果號(hào));
取出下一行,返回一個(gè)數(shù)組.可以用數(shù)字下標(biāo)訪問(wèn)(第一個(gè)字段是下標(biāo) 0),也可以用字符串下標(biāo)訪問(wèn)(即使用各字段名)。如已取了最后一行,返回 false.。
(6)mysql_fetch_row(結(jié)果號(hào));
返回一個(gè)矩陣代表結(jié)果集中一行的所有域。每次調(diào)用都會(huì)產(chǎn)生下一行,直到?jīng)]有行剩下時(shí)返回false。每個(gè)域值都由一個(gè)從零開(kāi)始的偏移量索引。這是從查詢(xún)中獲取結(jié)果的最快方法。
(7)integer mysql_num_rows(結(jié)果號(hào));
返回結(jié)果集中行的數(shù)目
(8)integer mysql_num_fields(結(jié)果號(hào));
返回結(jié)果集中域的數(shù)目。
(9)integer mysql_list_dbs();
向服務(wù)器查詢(xún)數(shù)據(jù)庫(kù)列表。它返回一個(gè)結(jié)果指針,該指針可用于mysql_fetch_row函數(shù)及類(lèi)似函數(shù)。
(10)mysql_list_tables(數(shù)據(jù)庫(kù)名);
獲取一個(gè)指向指定數(shù)據(jù)庫(kù)的表單列表的結(jié)果指針。該結(jié)果指針可用于任何從結(jié)果集中獲取行的函數(shù)。
(11)mysql_close(連接號(hào));
關(guān)閉對(duì)數(shù)據(jù)庫(kù)的連接。連接必須是由mysql_connect打開(kāi)的。該函數(shù)的使用不是嚴(yán)格必需的,因?yàn)樵谀_本結(jié)束時(shí),所有非永久鏈路都會(huì)被自動(dòng)關(guān)閉。
(12)mysql_pconnect(主機(jī),用戶(hù)名,口令);
與mysql_connect完全相似,但建立一個(gè)"永久連接",該連接一經(jīng)建立永不關(guān)閉,即使使用mysql_close函數(shù)或程序執(zhí)行完畢也不關(guān)閉.下一次試圖建立永久連接時(shí),系統(tǒng)如發(fā)現(xiàn)已存在一個(gè)永久連接,則直接返回該連接號(hào)而不重新創(chuàng)建。
下面是一個(gè)調(diào)用MYSQL數(shù)據(jù)庫(kù)并分頁(yè)顯示的例子。
?
$pagesize = 5; //每頁(yè)顯示5條記錄
$host="localhost";
$user="user";
$password="psw";
$dbname="book"; //所查詢(xún)的庫(kù)表名;
//連接MySQL數(shù)據(jù)庫(kù)
mysql_connect("$host","$user","$password") or die("無(wú)法連接MySQL數(shù)據(jù)庫(kù)服務(wù)器!");
$db = mysql_select_db("$dbname") or die("無(wú)法連接數(shù)據(jù)庫(kù)!");
$sql = "select count(*) as total from pagetest";//生成查詢(xún)記錄數(shù)的SQL語(yǔ)句
$rst = mysql_query($sql) or die("無(wú)法執(zhí)行SQL語(yǔ)句:$sql !"); //查詢(xún)記錄數(shù)
$row = mysql_fetch_array($rst) or die("沒(méi)有更多的記錄!"); /取出一條記錄
$rowcount = $row["total"];//取出記錄數(shù)
mysql_free_result($rst) or die("無(wú)法釋放result資源!"); //釋放result資源
$pagecount = bcdiv($rowcount+$pagesize-1,$pagesize,0);//算出總共有幾頁(yè)
if(!isset($pageno)) {
$pageno = 1; //在沒(méi)有設(shè)置pageno時(shí),缺省為顯示第1頁(yè)
}
if($pageno1) {
$pageno = 1; //若pageno比1小,則把它設(shè)置為1
}
if($pageno$pagecount) {
$pageno = $pagecount; //若pageno比總共的頁(yè)數(shù)大,則把它設(shè)置為最后一頁(yè)
}
if($pageno0) {
$href = eregi_replace("%2f","/",urlencode($PHP_SELF));//把$PHP_SELF轉(zhuǎn)換為可以在URL上使用的字符串,這樣的話就可以處理中文目錄或中文文件名
if($pageno1){//顯示上一頁(yè)的褳接
echo "a href="" . $href . "?pageno=" . ($pageno-1) . ""上一頁(yè)/a ";
}
else{
echo "上一頁(yè)";
}
for($i=1;$i$pageno;$i++){
echo "a href="" . $href . "?pageno=" . $i . """ . $i . "/a ";
}
echo $pageno . " ";
for($i++;$i=$pagecount;$i++){
echo "a href="" . $href . "?pageno=" . $i . """ . $i . "/a ";
}
if($pageno$pagecount){//顯示下一頁(yè)的褳接
echo "a href="" . $href . "?pageno=" . ($pageno+1) . ""下一頁(yè)/a ";
}
else{
echo "下一頁(yè) ";
}
$offset = ($pageno-1) * $pagesize;//算出本頁(yè)第一條記錄在整個(gè)表中的位置(第一條記錄為0)
$sql = "select * from pagetest LIMIT $offset,$pagesize";//生成查詢(xún)本頁(yè)數(shù)據(jù)的SQL語(yǔ)句
$rst = mysql_query($sql);//查詢(xún)本頁(yè)數(shù)據(jù)
$num_fields = mysql_num_fields($rst);//取得字段總數(shù)
$i = 0;
while($i$num_fields){//取得所有字段的名字
$fields[$i] = mysql_field_name($rst,$i);//取得第i+1個(gè)字段的名字
$i++;
}
echo "table border="1" cellspacing="0" cellpadding="0"";//開(kāi)始輸出表格
echo "tr";
reset($fields);
while(list(,$field_name)=each($fields)){//顯示字段名稱(chēng)
echo "th$field_name/th";
}
echo "/tr";
while($row=mysql_fetch_array($rst)){//顯示本頁(yè)數(shù)據(jù)
echo "tr";
reset($fields);
while(list(,$field_name)=each($fields)){//顯示每個(gè)字段的值
$field_value = $row[$field_name];
if($field_value==""){
echo "td /td";
}
else{
echo "td$field_value/td";
}
}
echo "/tr";
}
echo "/table";//表格輸出結(jié)束
mysql_free_result($rst) or die("無(wú)法釋放result資源!");//釋放result資源
}
else{
echo "目前該表中沒(méi)有任何數(shù)據(jù)!";
}
mysql_close($server) or die("無(wú)法與服務(wù)器斷開(kāi)連接!");//斷開(kāi)連接并釋放資源
?
開(kāi)放數(shù)據(jù)庫(kù)連接(ODBC)已成為一種與數(shù)據(jù)庫(kù)進(jìn)行通信的工業(yè)標(biāo)準(zhǔn)。PHP也提供了標(biāo)準(zhǔn)的接口,使得PHP能調(diào)用Access,SQL SERVER等數(shù)據(jù)庫(kù)。其相關(guān)函數(shù)是:
(1)integer odbc_connect(string dsn, string user, string password)
連接到一個(gè)ODBC數(shù)據(jù)庫(kù)源名字上。
(2)integer odbc_exec(integer connection, string query)或 odbc_do(integer connection, string query)
在一個(gè)連接上執(zhí)行查詢(xún)。
(3)boolean odbc_fetch_row(integer result, integer row)
從一個(gè)結(jié)果集中獲取一行數(shù)據(jù)。Row參數(shù)是可選的,若為空缺,則返回下一個(gè)有效行。在結(jié)果集中不再剩余行時(shí)返回false。
(4)boolean odbc_close(integer connection)
關(guān)閉一個(gè)數(shù)據(jù)庫(kù)的連接。若在該連接上有打開(kāi)的事務(wù),則返回一個(gè)錯(cuò)誤,而且連接不會(huì)被關(guān)閉。
最后,還是看個(gè)分頁(yè)的例子:
?
//設(shè)定每頁(yè)顯示條數(shù)
$show_num = 10;
$spages = $pages;//避免$pages后期被改變
//定義連接
$dsn = "localhost";
$user = "sa";
$password = "";
//計(jì)算總記錄數(shù)
$rs_num = "select count(*) as id from bbs where zu='0' and lei='".$lei."'";
$conn_id = odbc_connect($dsn,$user,$password);
$rnum = odbc_exec($conn_id,$rs_num);
while(odbc_fetch_row($rnum)){
$total_rs = odbc_result($rnum,"id");//將總記錄數(shù)放入$total_rs變量
}
//計(jì)算與頁(yè)有關(guān)的條數(shù)
$nnn = $total_rs / $show_num;//計(jì)算總頁(yè)數(shù)
$hnnn = intval($nnn);//將總頁(yè)數(shù)取整
$cnnnn = $nnn - $hnnn;
//計(jì)算所需總頁(yè)數(shù)
switch ($cnnn){
case "0":
$hnnn++;
$nnn = $hnnn;//總頁(yè)數(shù)
break;
default :
$nnn = $hnnn;//總頁(yè)數(shù)
break;
};
if ($nnn == 0)$nnn++;
//計(jì)算頁(yè)面改變所需的條件
$fore = $pages;
$next = $pages;
$fore -= 1;
$next += 1;
if ($fore 0) {
echo "a首頁(yè)/a";
echo "a前頁(yè)/a";
};
if ($pages $nnn) {
echo "a后頁(yè)/a";
echo "a尾頁(yè)/a";
};
echo "共".$nnn."頁(yè)";
$query_string = "SELECT * FROM table where condition order by you wanted order";
$cur = odbc_exec($conn_id,$query_string);
//取到循環(huán)的頂部
$cnum = ($pages-1) * $show_num;//計(jì)算當(dāng)前的記錄游標(biāo)的位置
//空循環(huán)到顯示記錄游標(biāo)處
if ($cnum != 0){
for ($i=0;$i=$cnum;odbc_fetch_row($cur));
};
$i=1;
//顯示記錄
while(odbc_fetch_row($cur)){
echo ;
if ($i == $show_num){//在不滿(mǎn)頁(yè)數(shù)時(shí)跳出程序
break;
};
$i++;
};
//關(guān)閉連接
odbc_close($conn_id);
?
Oracle(甲骨文)是世界上最為流行的關(guān)系數(shù)據(jù)庫(kù)。它是大公司推崇的工業(yè)化的強(qiáng)有力的引擎。我們先看看其相關(guān)的函數(shù):
(1)integer ora_logon(string user , string password)
開(kāi)始對(duì)一個(gè)Oracle數(shù)據(jù)庫(kù)服務(wù)器的連接。
(2)integer ora_open(integer connection)
打開(kāi)給出的連接的游標(biāo)。
(3)integer ora_do(integer connection, string query)
在給出的連接上執(zhí)行查詢(xún)。PHP生成一個(gè)指示器,解析查詢(xún),并執(zhí)行之。
(4)integer ora_parse(integer cursor, string query)
解析一個(gè)查詢(xún)并準(zhǔn)備好執(zhí)行。
(5)boolean ora_exec(integer cursor)
執(zhí)行一個(gè)先前由ora_parse函數(shù)解析過(guò)的查詢(xún)。
(6)boolean ora_fetch(integer cursor)
此函數(shù)會(huì)使得一個(gè)執(zhí)行過(guò)的查詢(xún)中的行被取到指示器中。這使得您可以調(diào)用ora_getcolumn函數(shù)。
(7)string ora_getcolumn(integer cursor, integer column)
返回當(dāng)前的值。列由零開(kāi)始的數(shù)字索引。
(8)boolean ora_logoff(integer connection)
斷開(kāi)對(duì)數(shù)據(jù)庫(kù)服務(wù)器的鏈接。
以下是向ORACLE數(shù)據(jù)庫(kù)插入數(shù)據(jù)的示例程序:
html
headtitle向ORACLE數(shù)據(jù)庫(kù)中插入數(shù)據(jù)/title/head
body
form action="?echo $PHP_SELF;?" method="post"
table border="1" cellspacing="0" cellpadding="0"
tr
thID/th
thname/th
thDescription/th
/tr
tr
tdinput type="text" name="name" maxlength="50" size="10"/td
tdinput type="text" name="email" maxlength="255" size="30"/td
tdinput type="text" name="Description" maxlength="255" size="50"/td
/tr
tr align="center"
td colspan="3"input type="submit" value="提交"??input type="reset" value="重寫(xiě)"/td
/tr
/table
/form
?
//先設(shè)置兩個(gè)環(huán)境變量ORACLE_HOME,ORACLE_SID
putenv("ORACLE_HOME=/oracle/app/oracle/product/8.0.4");
putenv("ORACLE_SID=ora8");
//設(shè)置網(wǎng)頁(yè)顯示中文
putenv("NLS_LANG=Simplified_Chinese.zhs16cgb231280");
if($connection=ora_logon("scott","tiger")) {
//庫(kù)表test有ID,name,Description三項(xiàng)
$sql = 'insert into test(ID,name,Description) values ';
$sql .= '('' . $ID . '','' . $name . '',''. $Description . '')';
if($cursor=ora_do($connect,$sql)) {
print("insert finished!");
}
$query = 'select * from test';
if($cursor=ora_do($connect,$query)) {
ora_fetch($cursor);
$content0=ora_getcolumn($cursor,0);
$content1=ora_getcolumn($cursor,1);
$content2=ora_getcolumn($cursor,2);
print("$content0");
print("$content1");
print("$content2");
ora_close($cursor);
}
ora_logoff($connection);
}
?
/body
/html
通過(guò)PHP你可以輕松的連接到數(shù)據(jù)庫(kù),請(qǐng)求數(shù)據(jù)并將其顯示在你的web站點(diǎn)中,甚至修改數(shù)據(jù)庫(kù)中的數(shù)據(jù)。 MySQL是一種很流行的數(shù)據(jù)庫(kù),并且在互聯(lián)網(wǎng)中有許多有關(guān)PHP與MySQL的教程。MySQL是免費(fèi)的,這一點(diǎn)也許就吸引了不少人。由于其廣泛應(yīng)用, 我就不想在這里贅述MySQL的使用方法了。Oracle被大量在企業(yè)應(yīng)用中采用,因此我們就利用Oracle來(lái)介紹PHP與數(shù)據(jù)庫(kù)的連接。我們當(dāng)然不會(huì) 提及Oracle數(shù)據(jù)庫(kù)的設(shè)計(jì)原理,原因是這已經(jīng)超出了我們的討論范圍。
PHP提供了兩套函數(shù)與Oracle連接,分別是ORA_和OCI函數(shù)。其中ORA_函數(shù)略顯陳舊。OCI函數(shù)更新?lián)f(shuō)更好一些。兩者的使用語(yǔ)法幾乎相差無(wú)幾。如前所述,你的PHP安裝選項(xiàng)應(yīng)該可以支持兩者的使用。
想獲得更多有關(guān)在Microsoft Windows平臺(tái)上安裝支持PHP3的Apache服務(wù)器的知識(shí)以及更多有關(guān)Oracle數(shù)據(jù)庫(kù)的知識(shí),請(qǐng)查閱以下URL:。
4.1 連接
if ($conn=Ora_Logon("user@TNSNAME","password"))
{
echo "SUCCESS ! Connected to database\n";
}
else
{
echo "Failed :-( Could not connect to database\n";
}
Ora_Logoff($conn);
phpinfo();
?
以上代碼使用TNSNAME(在你的tnsnames.ora文件中指明)定義的Oracle數(shù)據(jù)庫(kù)名稱(chēng)、用戶(hù)名稱(chēng)和密碼連接數(shù)據(jù)庫(kù)。在成功連接的基礎(chǔ)上,ora_logon函數(shù)返回一個(gè)非零的連接ID并儲(chǔ)存在變量$conn中。
4.2 查詢(xún)
假設(shè)與數(shù)據(jù)庫(kù)已經(jīng)連接就緒,下面我們就來(lái)實(shí)際的應(yīng)用對(duì)數(shù)據(jù)庫(kù)的查詢(xún)。下面的代碼演示了一個(gè)連接并查詢(xún)的典型例子:
/*
* 連接數(shù)據(jù)庫(kù)并執(zhí)行查詢(xún)
*/
function printoraerr($in_cur)
{
// 檢查Oracle是否出錯(cuò)
// 如果存在錯(cuò)誤則顯示
// 當(dāng)指針被激活時(shí)每次請(qǐng)求Oracle后調(diào)用該函數(shù)
if(ora_errorcode($in_cur))
echo "Oracle code - ".ora_error($in_cur)."\n";
return;
}
/** 主程序 */
if (!($conn=ora_logon("user@TNSNAME","password")))
{
echo "Connection to database failed\n";
exit;
}
echo "Connected as connection - $conn
\n";
echo "Opening cursor ...
\n";
$cursor=ora_open($conn); printoraerr($cursor);
echo "Opened cursor - $cursor
\n";
$qry="select user,sysdate from dual";
echo "Parsing the query $qry ...
\n";
ora_parse($cursor,$qry,0); printoraerr($cursor);
echo "Query parsed
\n";
echo "Executing cursor ...
\n";
ora_exec($cursor); printoraerr($cursor);
echo "Executed cursor
\n";
echo "Fetching cursor ...
\n";
while(ora_fetch($cursor))
{
$user=ora_getcolumn($cursor,0); printoraerr($cursor);
$sysdate=ora_getcolumn($cursor,1); printoraerr($cursor);
echo " row = $user, $sysdate
\n";
}
echo "Fetched all records
\n";
echo "Closing cursor ...
\n";
ora_close($cursor);
echo "Closed cursor
\n";
echo "Logging off from oracle...
\n";
ora_logoff($conn);
echo "Logged off from oracle
\n";
?
(譯者注:以上代碼段缺少注釋?zhuān)?qǐng)讀者參考PHP Manual的Oracle數(shù)據(jù)庫(kù)函數(shù)部分)
4.3 顯示結(jié)果
以下代碼演示了怎樣查詢(xún)數(shù)據(jù)庫(kù)并將結(jié)果輸出:
function printoraerr($in_cur, $conn)
{
// 檢查Oracle是否出錯(cuò)
// 如果存在錯(cuò)誤則顯示
// 當(dāng)指針被激活時(shí)每次請(qǐng)求Oracle后調(diào)用該函數(shù)
// If it encountered an error, we exit immediately
if(ora_errorcode($in_cur))
{
echo "Oracle code - ".ora_error($in_cur)."
n";
ora_logoff($conn);
exit;
}
return;
}
function exequery($w_qry,$conn)
{
$cursor=ora_open($conn); printoraerr($cursor,$conn);
ora_parse($cursor,$w_qry,0); printoraerr($cursor,$conn);
ora_exec($cursor); printoraerr($cursor,$conn);
$numrows=0;
$w_numcols=ora_numcols($cursor);
// 顯示頭部
echo "
\n";
for ($i=0;$i$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
echo "\t ".ora_columnname($cursor,$i)." \n";
}
echo "
\n";
while(ora_fetch($cursor))
{
echo " \n";
for ($i=0;$i$w_numcols;$i++)
{
$align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT";
if(ora_columntype($cursor,$i)=="LONG")
echo " ".
ora_getcolumn($cursor,$i)."
\n";
else
echo " ".ora_getcolumn($cursor,$i)." \n";
printoraerr($cursor,$conn);
}
$numrows++;
echo "
\n";
}
if ($numrows==0)
echo " Query returned no records
\n";
else
{
echo " \n";
echo " Count \n";
echo " $numrows \n";
echo "
\n";
}
echo " \n";
ora_close($cursor);
return;
}
// 主程序
if(!($conn=ora_logon("user@SID","password")))
{
echo "Error: Cannot connect to database\n";
exit;
}
$qry="SELECT
deptno \"Dept\"
,empno \"Emp\"
,empnm \"Name\"
,salary \"Salary\"
FROM
employee
ORDER BY 1,2";
exequery($qry);
ora_logoff($conn);
?
(譯者注:以上代碼段缺少注釋?zhuān)?qǐng)讀者參考PHP Manual的Oracle數(shù)據(jù)庫(kù)函數(shù)部分)
4.4 基于HTTP的Oracle登錄
將以下代碼加在PHP頁(yè)面代碼之前以確認(rèn)Oracle登錄。注意你必須正確設(shè)定$ SID。
if(!isset($PHP_AUTH_USER))
{
Header("WWW-authenticate: basic realm=\"$SID\"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "
You are not authorized to enter the site
\n";
exit;
}
else
{
if (!($conn=ora_logon("$PHP_AUTH_USER@$SID",$PHP_AUTH_PW)))
{
Header("WWW-authenticate: basic realm=\"$SID\"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "
You are not authorised to enter the site
\n";
exit;
}
}
?
本文實(shí)例講述了PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)。分享給大家供大家參考,具體如下:
目的:從sql
server數(shù)據(jù)庫(kù)里面把某個(gè)視圖文件調(diào)用出來(lái),以鍵值對(duì)的方式顯示在頁(yè)面上。
利用pdo
odbc來(lái)實(shí)現(xiàn)PHP連接數(shù)據(jù)庫(kù):
在PHP配置文件里面開(kāi)啟pdo_odbc.dll服務(wù)。重啟Apache服務(wù)器。
在ThinkPHP5.1的項(xiàng)目中在模塊里添加config添加規(guī)定好的樣式數(shù)據(jù)庫(kù):
代碼如下:
?php
return
[
//
數(shù)據(jù)庫(kù)類(lèi)型
'type'
=
'sqlsrv',
//
服務(wù)器地址
'hostname'
=
'localhost',
//
數(shù)據(jù)庫(kù)名
'database'
=
'mysql',
//
用戶(hù)名
'username'
=
'sa',
//
密碼
'password'
=
'123456',
//
端口
'hostport'
=
'',
//
連接dsn
'dsn'
=
'odbc:Driver={SQL
Server};Server=localhost;Database=mysql',
//
數(shù)據(jù)庫(kù)連接參數(shù)
'params'
=
[],
//
數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8
'charset'
=
'utf8',
//
數(shù)據(jù)庫(kù)表前綴
'prefix'
=
'',
//
數(shù)據(jù)庫(kù)調(diào)試模式
'debug'
=
true,
//
數(shù)據(jù)庫(kù)部署方式:0
集中式(單一服務(wù)器),1
分布式(主從服務(wù)器)
'deploy'
=
0,
//
數(shù)據(jù)庫(kù)讀寫(xiě)是否分離
主從式有效
'rw_separate'
=
false,
//
讀寫(xiě)分離后
主服務(wù)器數(shù)量
'master_num'
=
1,
//
指定從服務(wù)器序號(hào)
'slave_no'
=
'',
//
是否嚴(yán)格檢查字段是否存在
'fields_strict'
=
true,
//
數(shù)據(jù)集返回類(lèi)型
'resultset_type'
=
'array',
//
自動(dòng)寫(xiě)入時(shí)間戳字段
'auto_timestamp'
=
false,
//
時(shí)間字段取出后的默認(rèn)時(shí)間格式
'datetime_format'
=
'Y-m-d
H:i:s',
//
是否需要進(jìn)行SQL性能分析
'sql_explain'
=
false,
//
Builder類(lèi)
'builder'
=
'',
//
Query類(lèi)
'query'
=
'\\think\\db\\Query',
//
是否需要斷線重連
'break_reconnect'
=
false,
//
斷線標(biāo)識(shí)字符串
'break_match_str'
=
[],
];
?
在控制器controller里面建一個(gè)控制文件Test.php
代碼如下:
?php
namespace
app\index\controller;
use
think\Db;
use
think\Controller;
class
Test
extends
Controller
{
public
function
zz(){
$data=Db::view('View_2')-select();
echo
json_encode($data);
}
}
?
最后調(diào)用入口文件即可訪問(wèn)。
我的效果:
[{"111":"123","1112":"LLP","232":"1","ROW_NUMBER":"1"},{"111":"123","1112":"BB","232":"2","ROW_NUMBER":"2"}]
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend
FrameWork框架入門(mén)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:ThinkPHP實(shí)現(xiàn)多數(shù)據(jù)庫(kù)連接的解決方法tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)查詢(xún)的方法ThinkPHP3.1新特性之多數(shù)據(jù)庫(kù)操作更加完善tp5(thinkPHP5)框架連接數(shù)據(jù)庫(kù)的方法示例PHP7使用ODBC連接SQL
Server2008
R2數(shù)據(jù)庫(kù)示例【基于thinkPHP5.1框架】thinkPHP5實(shí)現(xiàn)的查詢(xún)數(shù)據(jù)庫(kù)并返回json數(shù)據(jù)實(shí)例tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫(kù)的方法tp5(thinkPHP5)框架數(shù)據(jù)庫(kù)Db增刪改查常見(jiàn)操作總結(jié)thinkPHP5框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)連接,跨數(shù)據(jù)連接查詢(xún)操作示例
PHP連接數(shù)據(jù)庫(kù)之PHP連接MYSQL數(shù)據(jù)庫(kù)代碼
?php???
$mysql_server_name='localhost';?
//改成自己的mysql數(shù)據(jù)庫(kù)服務(wù)器??
$mysql_username='root';?
//改成自己的mysql數(shù)據(jù)庫(kù)用戶(hù)名??
$mysql_password='12345678';?
//改成自己的mysql數(shù)據(jù)庫(kù)密碼??
$mysql_database='mycounter';
//改成自己的mysql數(shù)據(jù)庫(kù)名??
$conn=mysql_connect($mysql_server_name,
$mysql_username,$mysql_password,
$mysql_database);???
$sql='CREATE?DATABASE?mycounter?
DEFAULT?CHARACTER?SET?gbk?COLLATE?gbk_chinese_ci;???
';???
mysql_query($sql);???
$sql='CREATE?TABLE?`counter`?
(`id`?INT(255)?UNSIGNED?NOT?NULL?
AUTO_INCREMENT?,`count`?INT(255)?
UNSIGNED?NOT?NULL?DEFAULT?0,PRIMARY?KEY?
(?`id`?)?)?TYPE?=?innodb;';???
mysql_select_db($mysql_database,$conn);???
$result=mysql_query($sql);???
//echo?$sql;???
mysql_close($conn);???
echo?"Hello!數(shù)據(jù)庫(kù)mycounter已經(jīng)成功建立!";???
??
PHP連接數(shù)據(jù)庫(kù)之PHP連接ACCESS數(shù)據(jù)庫(kù)代碼方法
???
$conn?=?new?com("ADODB.Connection");???
$connstr?=?"DRIVER={Microsoft
Access?Driver?(*.mdb)};?
DBQ=".?realpath("data/db.mdb");???
$conn-Open($connstr);???
$rs?=?new?com("ADODB.RecordSet");???
$rs-Open("select?*
from?szd_t",$conn,1,1);???
while(!?$rs-eof)?{???
$f?=?$rs-Fields(1);???
echo?$f-value;???
$rs-MoveNext();???
}???
?
php支持的數(shù)據(jù)庫(kù)有很多噢,下面列舉一下!
MySQL
MySQL 是最流行的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),在 WEB 應(yīng)用方面 MySQL 是最好的 RDBMS(Relational Database Management System:關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng))應(yīng)用軟件之一。
MsSql
ms SQL是指微軟的SQLServer數(shù)據(jù)庫(kù)服務(wù)器,它是一個(gè)數(shù)據(jù)庫(kù)平臺(tái),提供數(shù)據(jù)庫(kù)的從服務(wù)器到終端的完整的解決方案,其中數(shù)據(jù)庫(kù)服務(wù)器部分,是一個(gè)數(shù)據(jù)庫(kù)管理系統(tǒng),用于建立、使用和維護(hù)數(shù)據(jù)庫(kù)。
Oracle
oracle是甲骨文公司的一款關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)。它是在數(shù)據(jù)庫(kù)領(lǐng)域一直處于領(lǐng)先地位的產(chǎn)品。可以說(shuō)Oracle數(shù)據(jù)庫(kù)系統(tǒng)是目前世界上流行的關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng),系統(tǒng)可移植性好、使用方便、功能強(qiáng),適用于各類(lèi)大、中、小、微機(jī)環(huán)境。它是一種高效率、可靠性好的、適應(yīng)高吞吐量的數(shù)據(jù)庫(kù)方案。
Access
Access是由微軟發(fā)布的關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)。它結(jié)合了 MicrosoftJet Database Engine 和 圖形用戶(hù)界面兩項(xiàng)特點(diǎn),是 Microsoft Office 的系統(tǒng)程序之一。
PostgreSQL
PostgreSQL是一種特性非常齊全的自由軟件的對(duì)象-關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(ORDBMS),是以加州大學(xué)計(jì)算機(jī)系開(kāi)發(fā)的POSTGRES,4.2版本為基礎(chǔ)的對(duì)象關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)。POSTGRES的許多領(lǐng)先概念只是在比較遲的時(shí)候才出現(xiàn)在商業(yè)網(wǎng)站數(shù)據(jù)庫(kù)中。
InterBase
InterBase是一種關(guān)系數(shù)據(jù)管理系統(tǒng)(Relational database management system RDBMS),它提供了在單機(jī)或多用戶(hù)環(huán)境中的快速數(shù)據(jù)處理及共享的工具。InterBase的核心是提供透明的多機(jī)種支持的網(wǎng)絡(luò)運(yùn)行服務(wù)器技術(shù)。InterBase是可以在Windows 95、Windows NT、Novell NetWare及多種UNIX操作系統(tǒng)上運(yùn)行的工具。
CUBRID
CUBRID是一個(gè)全面開(kāi)源,且完全免費(fèi)的關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)。
dBase
dBase是第一個(gè)在個(gè)人電腦上被廣泛使用的單機(jī)版數(shù)據(jù)庫(kù)系統(tǒng)。
Firebird/InterBase
Firebird特性介紹firebird是一個(gè)全功能的,強(qiáng)大高效的,輕量級(jí),免維護(hù)的數(shù)據(jù)庫(kù)。
IBM DB2
IBM DB2 是美國(guó)IBM公司開(kāi)發(fā)的一套關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)
Informix
Informix是IBM公司出品的關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)(RDBMS)家族。
MaxDB
MaxDB是一種企業(yè)級(jí)數(shù)據(jù)庫(kù)管理系統(tǒng)。
MongoDB
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù)。
mSQL
mSQL(mini SQL)是一個(gè)單用戶(hù)數(shù)據(jù)庫(kù)管理系統(tǒng),個(gè)人使用免費(fèi),商業(yè)使用收費(fèi)。由于它的短小精悍,使其開(kāi)發(fā)的應(yīng)用系統(tǒng)特別受到互聯(lián)網(wǎng)用戶(hù)青睞。
SQLite
SQLite,是一款輕型的數(shù)據(jù)庫(kù),是遵守ACID的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它包含在一個(gè)相對(duì)小的C庫(kù)中。
SQLSRV
SQL Server(SQLSRV )是由Microsoft開(kāi)發(fā)和推廣的關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)(RDBMS)。
Sybase
美國(guó)Sybase公司研制的一種關(guān)系型數(shù)據(jù)庫(kù)系統(tǒng),是一種典型的UNIX或WindowsNT平臺(tái)上客戶(hù)機(jī)/服務(wù)器環(huán)境下的大型數(shù)據(jù)庫(kù)系統(tǒng)。
tokyo_tyrant
一個(gè)可持久化數(shù)據(jù)的,好處是速度不錯(cuò),而且大至兼容Memcached的文本協(xié)議,客戶(hù)端可以繼續(xù)使用SpyMemcached。
希望對(duì)你有幫助,謝謝采納!
本文實(shí)例講述了PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫(kù)操作基類(lèi)。分享給大家供大家參考,具體如下:
配置文件:
?php
$db
=
array(
'host'='localhost',
'user'='root',
'password'='',
'database'='test',
)
?
php
數(shù)據(jù)庫(kù)基類(lèi):
?php
class
db
{
public
$conn;
public
static
$sql;
public
static
$instance=null;
private
function
__construct(){
require_once('db.config.php');
$this-conn
=
mysql_connect($db['host'],$db['user'],$db['password']);
if(!mysql_select_db($db['database'],$this-conn)){
echo
"失敗";
};
mysql_query('set
names
utf8',$this-conn);
}
public
static
function
getInstance(){
if(is_null(self::$instance)){
self::$instance
=
new
db;
}
return
self::$instance;
}
/**
*
查詢(xún)數(shù)據(jù)庫(kù)
*/
public
function
select($table,$condition=array(),$field
=
array()){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
$fieldstr
=
'';
if(!empty($field)){
foreach($field
as
$k=$v){
$fieldstr.=
$v.',';
}
$fieldstr
=
rtrim($fieldstr,',');
}else{
$fieldstr
=
'*';
}
self::$sql
=
"select
{$fieldstr}
from
{$table}
{$where}";
$result=mysql_query(self::$sql,$this-conn);
$resuleRow
=
array();
$i
=
0;
while($row=mysql_fetch_assoc($result)){
foreach($row
as
$k=$v){
$resuleRow[$i][$k]
=
$v;
}
$i++;
}
return
$resuleRow;
}
/**
*
添加一條記錄
*/
public
function
insert($table,$data){
$values
=
'';
$datas
=
'';
foreach($data
as
$k=$v){
$values.=$k.',';
$datas.="'$v'".',';
}
$values
=
rtrim($values,',');
$datas
=
rtrim($datas,',');
self::$sql
=
"INSERT
INTO
{$table}
({$values})
VALUES
({$datas})";
if(mysql_query(self::$sql)){
return
mysql_insert_id();
}else{
return
false;
};
}
/**
*
修改一條記錄
*/
public
function
update($table,$data,$condition=array()){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
$updatastr
=
'';
if(!empty($data)){
foreach($data
as
$k=$v){
$updatastr.=
$k."='".$v."',";
}
$updatastr
=
'set
'.rtrim($updatastr,',');
}
self::$sql
=
"update
{$table}
{$updatastr}
{$where}";
return
mysql_query(self::$sql);
}
/**
*
刪除記錄
*/
public
function
delete($table,$condition){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
self::$sql
=
"delete
from
{$table}
{$where}";
return
mysql_query(self::$sql);
}
public
static
function
getLastSql(){
echo
self::$sql;
}
}
$db
=
db::getInstance();
//$list
=
$db-select('demo',array('name'='tom','password'='ds'),array('name','password'));
//echo
$db-insert('demo',array('name'='腳本之家','password'='123'));
//echo
$db-update('demo',array("name"='xxx',"password"='123'),array('id'=1));
echo
$db-delete('demo',array('id'='2'));
db::getLastSql();
echo
"pre";
?
更多關(guān)于PHP操作數(shù)據(jù)庫(kù)相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
當(dāng)前題目:基于數(shù)據(jù)庫(kù)的php,基于數(shù)據(jù)庫(kù)的電商平臺(tái)總結(jié)
標(biāo)題鏈接:http://chinadenli.net/article22/dsihscc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站收錄、面包屑導(dǎo)航、域名注冊(cè)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)