php鏈接數(shù)據(jù)庫表單,php 數(shù)據(jù)庫連接

PHP連接數(shù)據(jù)庫的幾種方法

用ASP連接各種數(shù)據(jù)庫的方法

創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)安多,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

一、ASP的對(duì)象存取數(shù)據(jù)庫方法

在ASP中,用來存取數(shù)據(jù)庫的對(duì)象統(tǒng)稱ADO(Active Data Objects),主要含有三種對(duì)象:Connection、Recordset 、Command

Connection:負(fù)責(zé)打開或連接數(shù)據(jù)

Recordset:負(fù)責(zé)存取數(shù)據(jù)表

Command:負(fù)責(zé)對(duì)數(shù)據(jù)庫執(zhí)行行動(dòng)查詢命令

二、連接各數(shù)據(jù)庫的驅(qū)動(dòng)程序

連接各數(shù)據(jù)庫可以使用驅(qū)動(dòng)程序,也可以使用數(shù)據(jù)源,不過我建議大家使用驅(qū)動(dòng)程序,因?yàn)槭褂抿?qū)動(dòng)程序非常方便、簡單,而使用數(shù)據(jù)源比較麻煩。

ODBC鏈接

適合數(shù)據(jù)庫類型 鏈接方式

access "Driver={microsoft access driver(*.mdb)};dbq=*.mdb;uid=admin;pwd=pass;"

dBase "Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"

Oracle "Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"

MSSQL server "Driver={sql server};server=servername;database=dbname;uid=sa;pwd=pass;"

MS text "Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"

Visual Foxpro "Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"

MySQL "Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=16386;"

OLEDB鏈接

適合的數(shù)據(jù)庫類型 鏈接方式

access "Provider=microsoft.jet.oledb.4.0;data source=your_database_path;user id=admin;password=pass;"

Oracle "Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"

MS SQL Server "Provider=SQLOLEDB;data source=machinename;initial catalog=dbname;userid=sa;password=pass;"

MS text "Provider=microsof.jet.oledb.4.0;data source=your_path;Extended Properties′text;FMT=Delimited′"

而我們?cè)谝话闱闆r下使用Access的數(shù)據(jù)庫比較多,在這里我建議大家連接Access數(shù)據(jù)庫使用下面的方法:

dim conn

set conn = server.createobject("adodb.connection")

conn.open = "provider=microsoft.jet.oledb.4.0;" "data source = " server.mappath("../db/bbs.mdb")

其中../db/bbs.mdb是你的數(shù)據(jù)庫存放的相對(duì)路徑!如果你的數(shù)據(jù)庫和ASP文件在同一目錄下,你只要這樣寫就可以了:

dim conn

set conn = server.createobject("adodb.connection")

conn.open = "provider=microsoft.jet.oledb.4.0;" "data source = " server.mappath("bbs.mdb")

有許多初學(xué)者在遇到數(shù)據(jù)庫連接時(shí)總是會(huì)出問題,然而使用上面的驅(qū)動(dòng)程序只要你的數(shù)據(jù)庫路徑選對(duì)了就不會(huì)出問題了。

PHP加數(shù)據(jù)庫

把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫

現(xiàn)在,我們創(chuàng)建一個(gè) HTML 表單,這個(gè)表單可把新記錄插入 "Persons" 表。

這是這個(gè) HTML 表單:

1

2

3

4

5

6

7

8

9

10

11

12

html

body

form action="insert.php" method="post"

Firstname: input type="text" name="firstname" /

Lastname: input type="text" name="lastname" /

Age: input type="text" name="age" /

input type="submit" /

/form

/body

/html

當(dāng)用戶點(diǎn)擊上例中 HTML 表單中的提交按鈕時(shí),表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會(huì)添加到數(shù)據(jù)庫表中。

下面是 "insert.php" 頁面的代碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("my_db", $con);

$sql="INSERT INTO Persons (FirstName, LastName, Age)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))

{

die('Error: ' . mysql_error());

}

echo "1 record added";

mysql_close($con)

?

怎么將php與數(shù)據(jù)庫連接

php鏈接mysql必備條件:

已安裝mysql數(shù)據(jù)庫;

檢查php環(huán)境是否已開啟mysql擴(kuò)展(一般情況下是開啟的);

檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項(xiàng);b.打開php.ini文件,檢查php_mysql.dll前分號(hào)是否已取掉。

php鏈接代碼如下:

?php

//設(shè)置編碼格式

header("Content-type:text/html;charset=utf-8");

//定義數(shù)據(jù)庫主機(jī)地址

$host="localhost";

//定義mysql數(shù)據(jù)庫登錄用戶名

$user="root";

//定義mysql數(shù)據(jù)庫登錄密碼

$pwd="";

//鏈接數(shù)據(jù)庫

$conn = mysql_connect($host,$user,$pwd);

//對(duì)連接進(jìn)行判斷

if(!$conn){

die("數(shù)據(jù)庫連接失??!".mysql_errno());

}else{

echo "數(shù)據(jù)庫連接成功!";

}

?

php連接mysql數(shù)據(jù)庫

?function

conn(){$conn01

=

mysql_connect("localhost",'root','123456');//root是帳號(hào),123456是密碼$mycon=mysql_select_db('testdatabase',$conn01);

//testdatabase是mysql數(shù)據(jù)庫名if($mycon){echo("數(shù)據(jù)庫連接成功");}else{echo("數(shù)據(jù)庫連接失敗");}}conn();?

php登錄頁面完整代碼連接數(shù)據(jù)庫

創(chuàng)建conn.php,連接數(shù)據(jù)庫。

$dns = 'mysql:host=127.0.0.1;dbname=test';

$username = 'root';

$password = 'root';

// 1.連接數(shù)據(jù)庫,創(chuàng)建PDO對(duì)象

$pdo = new PDO($dns,$username,$password);

創(chuàng)建login.html,登陸頁面。

用戶名

密 碼

創(chuàng)建login.php,驗(yàn)證賬號(hào)密碼。

header("Content-Type: text/html; charset=utf8");

if(!isset($_POST["submit"])){

exit("錯(cuò)誤執(zhí)行");

}//檢測是否有submit操作

include('conn.php');//鏈接數(shù)據(jù)庫

$name = $_POST['name'];//post獲得用戶名表單值

$pwd = sha1($_POST['password']);//post獲得用戶密碼單值

if ($name $pwd){//如果用戶名和密碼都不為空

$sql = "select * from user where username = '$name' and password='$pwd'";//檢測數(shù)據(jù)庫是否有對(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頁面

exit;

}else{

echo "用戶名或密碼錯(cuò)誤";

echo "

setTimeout(function(){window.location.href='login.html';},1000);

";//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;

}

}else{//如果用戶名或密碼有空

echo "表單填寫不完整";

echo "

setTimeout(function(){window.location.href='login.html';},1000);

";

//如果錯(cuò)誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;

}

$pdo = null;

創(chuàng)建signup.html,注冊(cè)頁面

用戶名:

密 碼:

創(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ù)庫

$sql="insert into user(id,username,password) values (null,'$name','$pwd')";//向數(shù)據(jù)庫插入表單傳來的值的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ù)庫

分享名稱:php鏈接數(shù)據(jù)庫表單,php 數(shù)據(jù)庫連接
文章地址:http://muchs.cn/article18/phipgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、App開發(fā)網(wǎng)頁設(shè)計(jì)公司、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)