需要PHP基礎知識和數據庫基礎知識。
創新互聯主要從事成都做網站、成都網站建設、網頁設計、企業做網站、公司建網站等業務。立足成都服務興縣,10多年網站建設經驗,價格優惠、服務專業,歡迎來電咨詢建站服務:18980820575
以SQL為例。使用PHP MySQL 函數可以編輯數據庫。
mysql_connect() 函數打開MySQL 連接。舉例
?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}// 一些代碼...mysql_close($con);
?
mysql_connect()三個參數分別是服務器名,連接賬號,連接密碼。
連接之后,可以使用mysql_select_db()設置要處理的數據庫,后面則是用數據庫語句處理數據。SQL語法簡介網頁鏈接
php鏈接mysql必備條件:
已安裝mysql數據庫;
檢查php環境是否已開啟mysql擴展(一般情況下是開啟的);
檢查方法:a.使用phpinfo();函數,看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。
php鏈接代碼如下:
?php
//設置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數據庫主機地址
$host="localhost";
//定義mysql數據庫登錄用戶名
$user="root";
//定義mysql數據庫登錄密碼
$pwd="";
//鏈接數據庫
$conn = mysql_connect($host,$user,$pwd);
//對連接進行判斷
if(!$conn){
die("數據庫連接失敗!".mysql_errno());
}else{
echo "數據庫連接成功!";
}
?
?php
mysql_connect("localhost","root","123456") //填寫mysql用戶名和密碼
or die("Could not connect to MySQL server!");
mysql_select_db("phpcms") //數據庫名
or die("Could not select database!");
mysql_query('set names "gbk"'); //數據庫內數據的編碼
?
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
headtitle向ORACLE數據庫中插入數據/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
?
//先設置兩個環境變量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))
{
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
本文標題:php如何引用數據庫,php怎么調用數據庫
URL地址:http://vcdvsql.cn/article28/heodjp.html
成都網站建設公司_創新互聯,為您提供手機網站建設、外貿建站、網站策劃、移動網站建設、微信小程序、云服務器
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯