PHP訪問MySQL數(shù)據(jù)庫:

目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、弓長嶺網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
因?yàn)檫B接數(shù)據(jù)庫需要較長的時(shí)間和較大的資源開銷,所以如果在多個(gè)網(wǎng)頁中都要頻繁地訪問數(shù)據(jù)庫,則可以建立與數(shù)據(jù)庫的持續(xù)連接。即調(diào)用mysql_pconnect()代替mysql_connect()。
基本步驟:
1.連接服務(wù)器:mysql_connect();
2.選擇數(shù)據(jù)庫:mysql_select_db();
3.執(zhí)行SQL語句:mysql_query();
查詢:select
顯示:show
插入:insert into
更新:update
刪除:delete
4.關(guān)閉結(jié)果集:mysql_free_result($result);
5.關(guān)閉數(shù)據(jù)庫:mysql_close($link);
PHP連接數(shù)據(jù)庫Mysql錯(cuò)誤:Fatal error: Call to undefined function mysql_connect() in。。。(某個(gè)php文件的地址)
此錯(cuò)誤是出現(xiàn)在安裝并配置好PHP、Mysql、Apache服務(wù)器的情況下產(chǎn)生的。
我在網(wǎng)上查找了很多原因,并嘗試著修改之后成功了,將解決方法跟大家交流下。
1、在PHP安裝目錄之下,找到php.ini,在此文件中將“;extension=php_mysql.dll”引號(hào)中句子前的分號(hào)去掉。表明php可以加載此mysql模塊,當(dāng)然也可以選擇其他要加載的模塊,去掉其前面的分號(hào),但是模塊加載得越多,占用的資源越多。
2、加載了模塊之后,就需要指明模塊所在的位置,比較簡單的方式是直接修改環(huán)境變量。
具體操作為:
右鍵”我的電腦” - “屬性” - “高級(jí)” - “環(huán)境變量”,編輯“系統(tǒng)變量”下的“Path”,在原來的值后加上“;E:\php\php5;E:\php\php5\ext”。(“E:\php\php5”為php的安裝目錄)
系統(tǒng)路徑添加好之后需要重啟計(jì)算機(jī)才能生效。
3、將php安裝目錄下的libmysql.dll拷貝至apache服務(wù)器的bin目錄之下。
4、所有的配置修改好之后,需要重啟apache服務(wù)器。
測試代碼:test.php(其中,user為MySQL的用戶名,userpassword為MySQL的對應(yīng)用戶密碼)
Oracle(甲骨文)是世界上最為流行的關(guān)系數(shù)據(jù)庫。它是大公司推崇的工業(yè)化的強(qiáng)有力的引擎。我們先看看其相關(guān)的函數(shù):
(1)integer
ora_logon(string
user
,
string
password)
開始對一個(gè)Oracle數(shù)據(jù)庫服務(wù)器的連接。
(2)integer
ora_open(integer
connection)
打開給出的連接的游標(biāo)。
(3)integer
ora_do(integer
connection,
string
query)
在給出的連接上執(zhí)行查詢。PHP生成一個(gè)指示器,解析查詢,并執(zhí)行之。
(4)integer
ora_parse(integer
cursor,
string
query)
解析一個(gè)查詢并準(zhǔn)備好執(zhí)行。
(5)boolean
ora_exec(integer
cursor)
執(zhí)行一個(gè)先前由ora_parse函數(shù)解析過的查詢。
(6)boolean
ora_fetch(integer
cursor)
此函數(shù)會(huì)使得一個(gè)執(zhí)行過的查詢中的行被取到指示器中。這使得您可以調(diào)用ora_getcolumn函數(shù)。
(7)string
ora_getcolumn(integer
cursor,
integer
column)
返回當(dāng)前的值。列由零開始的數(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è)置兩個(gè)環(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三項(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
在mysql數(shù)據(jù)庫中,創(chuàng)建一個(gè)test數(shù)據(jù)庫,用于測試。
請點(diǎn)擊輸入圖片描述
新建一個(gè)php文件,命名為test.php,用于講解php如何選擇要操作的數(shù)據(jù)庫。
請點(diǎn)擊輸入圖片描述
在test.php文件中,使用header()方法將頁面的編碼格式設(shè)置為utf-8,避免輸出中文亂碼。
請點(diǎn)擊輸入圖片描述
在test.php文件中,使用mysql_connect()函數(shù),通過賬號(hào)和密碼創(chuàng)建一個(gè)數(shù)據(jù)庫的連接。
請點(diǎn)擊輸入圖片描述
在test.php文件中,再使用mysql_select_db()函數(shù)選擇要操作的數(shù)據(jù)庫test,選擇數(shù)據(jù)庫成功,則返回true,否則,返回false。最后,通過if語句判斷結(jié)果。
請點(diǎn)擊輸入圖片描述
在瀏覽器打開test.php文件,查看結(jié)果。
請點(diǎn)擊輸入圖片描述
END
總結(jié):
1、創(chuàng)建一個(gè)test數(shù)據(jù)庫。
2、使用mysql_connect()函數(shù)創(chuàng)建一個(gè)數(shù)據(jù)庫的連接。
3、再使用mysql_select_db()函數(shù)選擇要操作的數(shù)據(jù)庫test,并通過if語句判斷結(jié)果。
網(wǎng)站題目:php訪問數(shù)據(jù)庫的 php訪問數(shù)據(jù)庫的代碼
URL分享:http://www.chinadenli.net/article38/dooigpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營銷、外貿(mào)建站、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)