看你用的什么數(shù)據(jù)庫(kù)了,如果是MySQL,只需要調(diào)用PHP內(nèi)置的mysql_connect函數(shù)就可以。
十年的王屋網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷(xiāo)網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整王屋建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“王屋網(wǎng)站設(shè)計(jì)”,“王屋網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
格式是 mysql_connect(servername,username,password);
現(xiàn)在比較流行的云數(shù)據(jù)庫(kù)更簡(jiǎn)單了,require第三方的sdk后直接調(diào)第三方提供給你的API就可以
創(chuàng)建conn.php,連接數(shù)據(jù)庫(kù)。
$dns = 'mysql:host=127.0.0.1;dbname=test';
$username = 'root';
$password = 'root';
// 1.連接數(shù)據(jù)庫(kù),創(chuàng)建PDO對(duì)象
$pdo = new PDO($dns,$username,$password);
創(chuàng)建login.html,登陸頁(yè)面。
用戶名
密 碼
創(chuàng)建login.php,驗(yàn)證賬號(hào)密碼。
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"])){
exit("錯(cuò)誤執(zhí)行");
}//檢測(cè)是否有submit操作
include('conn.php');//鏈接數(shù)據(jù)庫(kù)
$name = $_POST['name'];//post獲得用戶名表單值
$pwd = sha1($_POST['password']);//post獲得用戶密碼單值
if ($name $pwd){//如果用戶名和密碼都不為空
$sql = "select * from user where username = '$name' and password='$pwd'";//檢測(cè)數(shù)據(jù)庫(kù)是否有對(duì)應(yīng)的username和password的sql
$stmt = $pdo-prepare($sql);
$stmt-execute();
if($stmt-fetch(PDO::FETCH_BOUND)){//0 false 1 true
header("refresh:0;url=welcome.html");//如果成功跳轉(zhuǎn)至welcome.html頁(yè)面
exit;
}else{
echo "用戶名或密碼錯(cuò)誤";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁(yè)面重試;
}
}else{//如果用戶名或密碼有空
echo "表單填寫(xiě)不完整";
echo "
setTimeout(function(){window.location.href='login.html';},1000);
";
//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁(yè)面重試;
}
$pdo = null;
創(chuàng)建signup.html,注冊(cè)頁(yè)面
用戶名:
密 碼:
創(chuàng)建signup.php
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST['submit'])){
exit("錯(cuò)誤執(zhí)行");
}//判斷是否有submit操作
$name=$_POST['name'];//post獲取表單里的name
$pwd = sha1($_POST['password']);//post獲取表單里的password
include('conn.php');//鏈接數(shù)據(jù)庫(kù)
$sql="insert into user(id,username,password) values (null,'$name','$pwd')";//向數(shù)據(jù)庫(kù)插入表單傳來(lái)的值的sql
$stmt = $pdo-prepare($sql);
$stmt-execute();
$stmt-fetch(PDO::FETCH_BOUND);
if (!$stmt){
die('Error: ' . $stmt-getMessage());//如果sql執(zhí)行失敗輸出錯(cuò)誤
}else{
echo "注冊(cè)成功";//成功輸出注冊(cè)成功
}
$pdo = null;//關(guān)閉數(shù)據(jù)庫(kù)
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í)行查詢。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ù)解析過(guò)的查詢。
(6)boolean
ora_fetch(integer
cursor)
此函數(shù)會(huì)使得一個(gè)執(zhí)行過(guò)的查詢中的行被取到指示器中。這使得您可以調(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
?php
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='12345678';
$mysql_database='mycounter';
$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基礎(chǔ)知識(shí)和數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)。
以SQL為例。使用PHP MySQL 函數(shù)可以編輯數(shù)據(jù)庫(kù)。
mysql_connect() 函數(shù)打開(kāi)MySQL 連接。舉例
?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}// 一些代碼...mysql_close($con);
?
mysql_connect()三個(gè)參數(shù)分別是服務(wù)器名,連接賬號(hào),連接密碼。
連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫(kù),后面則是用數(shù)據(jù)庫(kù)語(yǔ)句處理數(shù)據(jù)。SQL語(yǔ)法簡(jiǎn)介網(wǎng)頁(yè)鏈接
在html中調(diào)用php內(nèi)容,可以用script src="friendlinks.php"/script然后在friendlinks.php中調(diào)取數(shù)據(jù)庫(kù)數(shù)據(jù)。并輸出適當(dāng)?shù)膆tml,或者輸出xml、json都可以,只是圖簡(jiǎn)單的話,只要輸出html就行了。
新聞標(biāo)題:PHP網(wǎng)頁(yè)如何調(diào)用數(shù)據(jù)庫(kù) PHP調(diào)用數(shù)據(jù)庫(kù)
瀏覽路徑:http://muchs.cn/article34/hjcepe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、企業(yè)建站、商城網(wǎng)站、虛擬主機(jī)、做網(wǎng)站、網(wǎng)站營(yíng)銷(xiāo)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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)