這里介紹php和mysql結(jié)合起來(lái)實(shí)用。
基本上,可以說(shuō)php是介于后臺(tái)數(shù)據(jù)庫(kù)和前臺(tái)瀏覽器的一個(gè)中間層,在他們之間傳遞命令。這種方式大大提高了交互的可能性,可以方便使用在投票系統(tǒng),其他動(dòng)態(tài)用戶輸入和個(gè)性化網(wǎng)站中。
要想實(shí)現(xiàn)這種交互,首先必需實(shí)現(xiàn)和mysql數(shù)據(jù)庫(kù)連接,可以使用這個(gè)命令實(shí)現(xiàn): 語(yǔ)法:int mysql_connect(string hostname, string username, string password);
·hostname - 運(yùn)行數(shù)據(jù)庫(kù)服務(wù)器所在的主機(jī)名稱。 ·username - 連接到數(shù)據(jù)庫(kù)服務(wù)器的用戶名稱。 ·Password - 用戶密碼。the password set to connect to the MySQL database. 如果連接成功,函數(shù)返回一個(gè)正整數(shù),如果失敗返回一個(gè)負(fù)數(shù)。
所有的命令,和往常一樣,必需放置在"<?" 和 "?>"之間。 讓我們繼續(xù)我們的工程,讓我們假設(shè)用MySQL建立了以下的數(shù)據(jù)表:
---------------------------- mysql> CREATE TABLE information ( -> name VARCHAR (25), -> email VARCHAR (25), -> choice VARCHAR (8) );
----------------------------
現(xiàn)在讓我們假設(shè)我們要吧用戶的信息插入到這個(gè)數(shù)據(jù)庫(kù)中,我們可以通過(guò)修改email.php3腳本來(lái)實(shí)現(xiàn),修改如下:
----------------------------
<? /* 這個(gè)腳本將使用從moreinfo.html文件中傳遞過(guò)來(lái)的變量。 */
/* 聲明一些相關(guān)的變量 */
$hostname = "devshed"; $username = "myusername"; $password = "mypassword"; $dbName = "mydbname";
/* 使用MySQL建立的數(shù)據(jù)表存取信息 */ $userstable = "information";
/* 網(wǎng)站管理員的郵件地址*/ $adminaddress = "administration@buycorn.com";
/* 與數(shù)據(jù)庫(kù)連接*/ MYSQL_CONNECT($hostname,$username,$password) OR DIE("Unable to connect to database");
@mysql_select_db("$dbName") or die("Unable to select database");
PRINT "<CENTER>"; PRINT "Hello, $name."; PRINT "<BR><BR>"; PRINT "Thank you for your interest.<BR><BR>"; PRINT "We will send information to $email, and have noted that you like $preference"; PRINT "</CENTER><BR><BR>";
/* 發(fā)送有關(guān)郵件*/ mail("$email", "Your request for information", "$namenThank you for your interest!n We sell fresh corn daily over the Internet! Place your order at http://www.buycorn.com, and receive a free package of $preference!");
mail("$adminaddress", "Visitor request for info.", "$name requested for information.n
The email address is $email. n The visitor prefers $preference.");
/* 將數(shù)據(jù)插入數(shù)據(jù)表中*/ $query = "INSERT INTO $userstable VALUES('$name','$email', '$preference')"; $result = MYSQL_QUERY($query);
PRINT "Your information has also been inserted into our database, for future reference.";
/* 關(guān)閉與數(shù)據(jù)庫(kù)的連接*/ MYSQL_CLOSE(); ?>
----------------------------
一些注意的地方:
1、在腳本一開(kāi)始聲明的變量是為了函數(shù)MYSQL_CONNECT() 。我們也可以直接在函數(shù)中將這些值給出,可是,考慮工程的可發(fā)展性,這些值應(yīng)該放在一個(gè)獨(dú)立的文件中,用(#include)調(diào)入。 2、語(yǔ)法: int mysql_select_db(string database_name, int link_identifier); ·database_name 必需是在服務(wù)器上的數(shù)據(jù)庫(kù)名。 ·link_identifier(可選的) 是指明連接,基于此向數(shù)據(jù)庫(kù)服務(wù)器發(fā)出請(qǐng)求。 ·返回值為true/false 3、語(yǔ)法: int mysql_query(string query, int link_identifier); ·query 發(fā)送向mysql服務(wù)器的sql命令。 ·link_identifier(可選擇) 指明連接,基于此向數(shù)據(jù)庫(kù)服務(wù)器發(fā)送sql命令。 ·返回值為整數(shù),正數(shù)表示成功了,負(fù)數(shù)表示失敗。 4、語(yǔ)法: int mysql_close(int link_identifier); ·link_indentifier 與上面相同 ·返回值為整數(shù),正數(shù)表示成功了,負(fù)數(shù)表示失敗。
在下一篇文章中,我將給大家講解如何從mysql中輸出數(shù)據(jù)。
|