十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
在mysql數(shù)據(jù)庫中,創(chuàng)建一個test數(shù)據(jù)庫,用于測試。
創(chuàng)新互聯(lián)服務(wù)項目包括天峻網(wǎng)站建設(shè)、天峻網(wǎng)站制作、天峻網(wǎng)頁制作以及天峻網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,天峻網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到天峻省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
請點擊輸入圖片描述
新建一個php文件,命名為test.php,用于講解php如何選擇要操作的數(shù)據(jù)庫。
請點擊輸入圖片描述
在test.php文件中,使用header()方法將頁面的編碼格式設(shè)置為utf-8,避免輸出中文亂碼。
請點擊輸入圖片描述
在test.php文件中,使用mysql_connect()函數(shù),通過賬號和密碼創(chuàng)建一個數(shù)據(jù)庫的連接。
請點擊輸入圖片描述
在test.php文件中,再使用mysql_select_db()函數(shù)選擇要操作的數(shù)據(jù)庫test,選擇數(shù)據(jù)庫成功,則返回true,否則,返回false。最后,通過if語句判斷結(jié)果。
請點擊輸入圖片描述
在瀏覽器打開test.php文件,查看結(jié)果。
請點擊輸入圖片描述
END
總結(jié):
1、創(chuàng)建一個test數(shù)據(jù)庫。
2、使用mysql_connect()函數(shù)創(chuàng)建一個數(shù)據(jù)庫的連接。
3、再使用mysql_select_db()函數(shù)選擇要操作的數(shù)據(jù)庫test,并通過if語句判斷結(jié)果。
Oracle(甲骨文)是世界上最為流行的關(guān)系數(shù)據(jù)庫。它是大公司推崇的工業(yè)化的強有力的引擎。我們先看看其相關(guān)的函數(shù):
(1)integer
ora_logon(string
user
,
string
password)
開始對一個Oracle數(shù)據(jù)庫服務(wù)器的連接。
(2)integer
ora_open(integer
connection)
打開給出的連接的游標。
(3)integer
ora_do(integer
connection,
string
query)
在給出的連接上執(zhí)行查詢。PHP生成一個指示器,解析查詢,并執(zhí)行之。
(4)integer
ora_parse(integer
cursor,
string
query)
解析一個查詢并準備好執(zhí)行。
(5)boolean
ora_exec(integer
cursor)
執(zhí)行一個先前由ora_parse函數(shù)解析過的查詢。
(6)boolean
ora_fetch(integer
cursor)
此函數(shù)會使得一個執(zhí)行過的查詢中的行被取到指示器中。這使得您可以調(diào)用ora_getcolumn函數(shù)。
(7)string
ora_getcolumn(integer
cursor,
integer
column)
返回當前的值。列由零開始的數(shù)字索引。
(8)boolean
ora_logoff(integer
connection)
斷開對數(shù)據(jù)庫服務(wù)器的鏈接。
以下是向ORACLE數(shù)據(jù)庫插入數(shù)據(jù)的示例程序:
html
headtitle向ORACLE數(shù)據(jù)庫中插入數(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="重寫"/td
/tr
/table
/form
?
//先設(shè)置兩個環(huán)境變量ORACLE_HOME,ORACLE_SID
putenv("ORACLE_HOME=/oracle/app/oracle/product/8.0.4");
putenv("ORACLE_SID=ora8");
//設(shè)置網(wǎng)頁顯示中文
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))
{
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
直接調(diào)用就行了,不過可能你需要引用文件,以下是例子
//文件conn.php,用于連接數(shù)據(jù)庫
class
DB_Conn
{
}
//文件
db.php,
用于數(shù)據(jù)庫操作,這個類必然需要使用數(shù)據(jù)庫連接對象,因此引用conn.php
require_once
conn.php;
class
DB
{
}
//文件user.php
require_once
'db.php';
class
User
{
public
function
getUserById($id)
{
$conn
=
new
Db_Conn();
$db
=
new
Db();
}
}以上只是示意,如果文件不在一個目錄下記得修改路徑。而且,一般來說數(shù)據(jù)庫對象應(yīng)該包含連接數(shù)據(jù)庫和數(shù)據(jù)操作的全部功能,不需要分別寫在兩個類里面。我覺得你對面向?qū)ο蟮睦斫膺€很淺薄,需要進一步累積經(jīng)驗。