if($field_value==""){
echo "<td> </td>";
}
else{
echo "<td>$field_value</td>";
}
}
echo "</tr>";
}
echo "</table>";//表格輸出結束
mysql_free_result($rst) or die("無法釋放result資源!");//釋放result資源
}
else{
echo "目前該表中沒有任何數據!";
}
mysql_close($server) or die("無法與服務器斷開連接!");//斷開連接并釋放資源
?>
開放數據庫連接(odbc)已成為一種與數據庫進行通信的工業標準。php也提供了標準的接口,使得php能調用access,sql server等數據庫。其相關函數是:
(1)integer odbc_connect(string dsn, string user, string password)
連接到一個odbc數據庫源名字上。
(2)integer odbc_exec(integer connection, string query)或 odbc_do(integer connection, string query)
在一個連接上執行查詢。
(3)boolean odbc_fetch_row(integer result, integer row)
從一個結果集中獲取一行數據。row參數是可選的,若為空缺,則返回下一個有效行。在結果集中不再剩余行時返回false。
(4)boolean odbc_close(integer connection)
關閉一個數據庫的連接。若在該連接上有打開的事務,則返回一個錯誤,而且連接不會被關閉。
最后,還是看個分頁的例子:
<?
//設定每頁顯示條數
$show_num = 10;
$spages = $pages;//避免$pages后期被改變
//定義連接
$dsn = "localhost";
$user = "sa";
$password = "";
//計算總記錄數
$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");//將總記錄數放入$total_rs變量
}
//計算與頁有關的條數
$nnn = $total_rs / $show_num;//計算總頁數
$hnnn = intval($nnn);//將總頁數取整
$cnnnn = $nnn - $hnnn;
//計算所需總頁數
switch ($cnnn){
case "0":
$hnnn++;
$nnn = $hnnn;//總頁數
break;
default :
$nnn = $hnnn;//總頁數
break;
};
if ($nnn == 0)$nnn++;
//計算頁面改變所需的條件
$fore = $pages;
$next = $pages;
$fore -= 1;
$next += 1;
if ($fore > 0) {
echo "<a>首頁</a>";
echo "<a>前頁</a>";
};
if ($pages < $nnn) {
echo "<a>后頁</a>";
echo "<a>尾頁</a>";
};
echo "共".$nnn."頁";
$query_string = "select * from table where condition order by you wanted order";
$cur = odbc_exec($conn_id,$query_string);
//取到循環的頂部
$cnum = ($pages-1) * $show_num;//計算當前的記錄游標的位置
//空循環到顯示記錄游標處
if ($cnum != 0){
for ($i=0;$i<=$cnum;odbc_fetch_row($cur)){$i++;};
};
$i=1;
//顯示記錄
while(odbc_fetch_row($cur)){
echo ;
if ($i == $show_num){//在不滿頁數時跳出程序
break;
};
$i++;
};
//關閉連接
odbc_close($conn_id);
?>
oracle(甲骨文)是世界上最為流行的關系數據庫。它是大公司推崇的工業化的強有力的引擎。我們先看看其相關的函數:
(1)integer ora_logon(string user , string password)
開始對一個oracle數據庫服務器的連接。
(2)integer ora_open(integer connection)
打開給出的連接的游標。
(3)integer ora_do(integer connection, string query)
在給出的連接上執行查詢。php生成一個指示器,解析查詢,并執行之。
(4)integer ora_parse(integer cursor, string query)
解析一個查詢并準備好執行。
(5)boolean ora_exec(integer cursor)
執行一個先前由ora_parse函數解析過的查詢。
(6)boolean ora_fetch(integer cursor)
此函數會使得一個執行過的查詢中的行被取到指示器中。這使得您可以調用ora_getcolumn函數。
(7)string ora_getcolumn(integer cursor, integer column)
返回當前的值。列由零開始的數字索引。
(8)boolean ora_logoff(integer connection)
斷開對數據庫服務器的鏈接。
以下是向oracle數據庫插入數據的示例程序:
<html>
<head><title>向oracle數據庫中插入數據</title></head>
<body>
<form action="<?echo $php_self;?>" method="post">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>id</th>
<th>name</th>
<th>description</th>
</tr>
<tr>
<td><input type="text" name="name" maxlength="50" size="10"></td>
<td><input type="text" name="email" maxlength="255" size="30"></td>
<td><input type="text" name="description" maxlength="255" size="50"></td>
</tr>
<tr align="center">
<td colspan="3"><input type="submit" value="提交"> <input type="reset" value="重寫"></td>
</tr>
</table>
</form>
<?
//先設置兩個環境變量oracle_home,oracle_sid
putenv("oracle_home=/oracle/app/oracle/product/8.0.4");
putenv("oracle_sid=ora8");
//設置網頁顯示中文
putenv("nls_lang=simplified_chinese.zhs16cgb231280");
if($connection=ora_logon("scott","tiger")) {
//庫表test有id,name,description三項
$sql = 'insert into test(id,name,description) values ';
$sql .= '('' . $id . '','' . $name . '',''. $description . '')';
if($cursor=ora_do($connect,$sql)) {
新聞熱點
疑難解答