目錄
onMouseOver" >onMouseOver
onMouseOver" >onMouseOver
首頁 後端開發 php教程 第十一节 数据库链接_PHP

第十一节 数据库链接_PHP

Jun 01, 2016 pm 12:38 PM
a align if name the 資料庫 連結

数据库链接

10. PHP最大的特色就是操作数据库的能力特别的强大,PHP提供对多种数据库的支持。

  通过PHP你可以轻松的连接到数据库,请求数据并将其显示在你的web站点中,甚至修改数据库中的数据。在这一节里我们主要以在互联网上跟PHP一起使用得最多的MySQL数据库为例,介绍一下相关的MySQL数据库的操作函数以及数据库的基本操作等方面的知识。

MySQL数据库中,我们用来连接数据库的函数有两个,它们分别为:
integer mysql_connect(string host,string user,string password);
integer mysql_pconnect(string host,string user,string password);
mysql_connect
函数和mysql_pconnect函数都是对指定主机上MySQL数据库的连接,如果该数据库位于一个不同的端口,则可以在主机名后加上冒号和端口号。函数的参数也可以缺省不填,如果不填参数,默认的主机名是“localhost”,用户名为数据库管理员,默认值为“root”,密码为空。与数据库连接成功之后,这两个函数都可以返回一个连接号,如果连接失败,则返回一个false值。让我们来看看下面几句语句:

$db=mysql_connect("localhost","user","password");
mysql_select_db("mydb",$db);
?>
注释:
$db=mysql_connect("localhost","user","password");
我们将mysql的链接参数,包括主机名、用户名和密码作为mysql_connect()的参数,同时得到返回值为$db,这样,在下面的语句中,我们就可以将变量$db作为一个连接mysql数据库的连接号来使用。
mysql_select_db("mydb",$db);
PHP程序链接到mydb数据库中,这样程序与数据库的链接就完成了。

10.1 一个简易的数据库留言簿

  在完成数据库的链接之后,我们就可以对数据库进行一系列的操作。下面是一个简易的数据库留言簿程序(guestbook.php3):

  我假设你机子上的MySQL数据库以及管理MYSQL数据库的工具 Phpmyadmin_2. 0.5都已经安装完成,并且可以正常工作。

我们要做的第一件事情是创建一个留言数据库,假定名字为: mydb

1、启动浏览器,打开Phpmyadmin_2. 0.5 的管理WEB界面。

2、在“Create new database”文本框内输入数据库名称mydb,然后按create按键。

  下一步,我们要在该留言数据库下创建一个数据表,假定名字为: guestbook

创建该数据表的命令如下所示:

CREATE TABLE guestbook (ID INT NOT NULL AUTO_INCREMENT, name CHAR(250), email CHAR(250), job CHAR(250), comments BLOB, PRIMARY KEY(ID));

最后,将下面的留言簿程序挎贝到你机子的可写目录下面,并保存成guestbook.php3文件。就这么简单,你已经有了自己的留言簿了。

10.2 留言簿程序(guestbook.php3):

php
/* $host : your MySQL-host, usually 'localhost' */
/* $user : your MYSQL-username */
/* $password : your MySQL-password */
/* $database : your MySQL-database */
/* $table : your MySQL-table */
/* $page_title : the title of your guestbook-pages */
/* $admin_mail : email-address of the administrator to send the new entries to */
/* $admin_name : the name of the administrator */
/* $html_mail : say yes if your mail-agent can handle HTML-mail, else say no */

$host = "localhost";
$user = "";
$password = "";
$database = "mydb";
$table = "guestbook";
$page_title = "pert guestbook";
$admin_mail = "pert@21cn.com";
$admin_name = "Webmaster";
$html_mail = "no";

?>


?<span class="SpellE">php</span>
echo $page_title; ?>



/* connect to the database */
mysql_pconnect("$host","$user","$password") or die("Can't connect to the SQL-server");
mysql_select_db("$database");

/* action=view : retrieve data from the database and show it to the user */
if($action == "view") {

/* function for showing the data */
function search_it($name) {

/* some vars */
global $offset,$total,$lpp,$dir;
global $table,$html_mail,$admin_name,$admin_mail;

/* select the data to get out of the database */
$query = "SELECT name, email, job, comments FROM $table";
$result = mysql_query($query);
$total= mysql_numrows($result);

print "

onMouseOver
="window.status='Add your name';return true" onMouseOut="window.status='';return true" TITLE="Add your name">加入留言

br>br>";

if ($total== 0) {
print "

此刻没人留言br>br>"; }

elseif ($total> 0) {

/* default */
$counter=0;
if ($dir=="") $dir="Next";
$lpp=5;
if ($offset==0) $offset=0;

if ($dir=="Next") {

if ($total > $lpp) {

$counter=$offset;
$offset+=$lpp;
$num=$offset;

if ($num > $total) {
$num=$total; } }

else {
$num=$total; } }

elseif ($dir=="Previous") {

if ($total > $lpp) {
$offset-=$lpp;

if ($offset $offset=0; }

$counter=$offset-$lpp;

if ($counter $counter=0;
$num=$counter+$lpp; }

else {
$num=$total; } }

while ($counter $j=0;
$j=$counter + 1;

/* now really grab the data */
$i1=mysql_result($result,$counter,"name");
$i2=mysql_result($result,$counter,"email");
$i3=mysql_result($result,$counter,"job");
$i4=mysql_result($result,$counter,"comments");

$i4 = stripslashes ("$i4");

/* print it in a nice layout */
print "

n";
print "
n";
print "
n";
print "
Name: $i1n";
print "
email:onMouseOver="window.status='Email $i2';return true" onMouseOut="window.status='';return true" TITLE="Email $i2">$i2n";
print "
Job: $i3n";
print "
Comment:n";
print "
$i4n";
print "
n";
print "
n";
$counter++;
}
}
mysql_close();
}

/* execute the function */
search_it($name);

/* See if we need to put on the NEXT or PREVIOUS buttons */
if ($total > $lpp) {
echo("

n");

/* See if we need a PREVIOUS button */
if ($offset > $lpp) {
echo("n"); }

/* See if we need a NEXT button */
if ($offset echo("n"); }

echo("n");
echo("n");
echo("");
}
}

/* action=add : show a form where the user can enter data to add to the database */
elseif($action == "add") { ?>





















请您填写留言


您的大名:

您的E-mail:


您的工作:


您的留言:




onMouseOver="window.status='Read all comments first';return true" onMouseOut="window.status='';return true" TITLE="Read all comments first">先观看所有的留言



}

/* action=send : add the data from the user into the database */
elseif($action == "send") {

/* check if a HTML-mail should be send or a plain/text mail */
if($html_mail == "yes") {
mail("$admin_name admin_mail
>","PHP3 Guestbook Addition","

$name ($email) schreefhetvolgendebericht in hetgastenboek :
$comments
您的留言: $name
您的大名: $email
您的email: $job
您的工作:
", "From: $name nReply-To: $name nContent-type: text/htmlnX-Mailer: PHP/" . phpversion());
}


/* MySQL really hates it when you try to put things with ' or " characters into a database, so strip these...*/
$comments = addslashes ("$comments");
$query = "INSERT INTO guestbook VALUES('','$name', '$email', '$job', '$comments')";
$result = MYSQL_QUERY($query);

?>

感谢, ?php echo $name; ?>, 您的留言.

onMouseOver

="window.status='View your comment now';return true" onMouseOut="window.status='';return true" TITLE="View your comment now">观看留言


}

/* if there's no action given, then we must show the main page */
else {

/* get the number of entries written into the guestbook*/
$query = "SELECT name from guestbook";
$result = MYSQL_QUERY($query);
$number = MYSQL_NUMROWS($result);

if ($number == "") {
$entry = "
还没有人留过言"; }

elseif ($number == "1") {
$entry = "
目前留言人数1"; }

else {
$entry = "
目前留言人数 $number "; }

echo "


";
echo "

$entry
";
echo "

onMouseOver

="window.status='请您留言';return true" onMouseOut="window.status='';return true" TITLE="Add your name to our guestbook">请您留言";

if ($number > "") {
echo "

onMouseOver

="window.status='观看留言';return true" onMouseOut="window.status='';return true" TITLE="View the names in our guestbook">观看留言"; }
echo "

";
}
?>

版权所有:onMouseOver="window.status='pert';return true" onMouseOut="window.status='';return true" TITLE="pert">无边天际



本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

2 個月不見,人形機器人 Walker S 會摺衣服了 2 個月不見,人形機器人 Walker S 會摺衣服了 Apr 03, 2024 am 08:01 AM

機器之能報道編輯:吳昕國內版的人形機器人+大模型組隊,首次完成疊衣服這類複雜柔性材料的操作任務。隨著融合了OpenAI多模態大模型的Figure01揭開神秘面紗,國內同行的相關進展一直備受關注。就在昨天,國內"人形機器人第一股"優必選發布了人形機器人WalkerS深入融合百度文心大模型後的首個Demo,展示了一些有趣的新功能。現在,得到百度文心大模型能力加持的WalkerS是這個樣子的。和Figure01一樣,WalkerS沒有走動,而是站在桌子後面完成一系列任務。它可以聽從人類的命令,折疊衣物

Go語言如何實作資料庫的增刪改查操作? Go語言如何實作資料庫的增刪改查操作? Mar 27, 2024 pm 09:39 PM

Go語言是一種高效、簡潔且易於學習的程式語言,因其在並發程式設計和網路程式設計方面的優勢而備受開發者青睞。在實際開發中,資料庫操作是不可或缺的一部分,本文將介紹如何使用Go語言實作資料庫的增刪改查操作。在Go語言中,我們通常會使用第三方函式庫來操作資料庫,例如常用的sql套件、gorm等。這裡以sql包為例介紹如何實作資料庫的增刪改查操作。假設我們使用的是MySQL資料庫。

iOS 18 新增「已復原」相簿功能 可找回遺失或損壞的照片 iOS 18 新增「已復原」相簿功能 可找回遺失或損壞的照片 Jul 18, 2024 am 05:48 AM

蘋果公司最新發布的iOS18、iPadOS18以及macOSSequoia系統為Photos應用程式增添了一項重要功能,旨在幫助用戶輕鬆恢復因各種原因遺失或損壞的照片和影片。這項新功能在Photos應用的"工具"部分引入了一個名為"已恢復"的相冊,當用戶設備中存在未納入其照片庫的圖片或影片時,該相冊將自動顯示。 "已恢復"相簿的出現為因資料庫損壞、相機應用未正確保存至照片庫或第三方應用管理照片庫時照片和視頻丟失提供了解決方案。使用者只需簡單幾步

Hibernate 如何實作多型映射? Hibernate 如何實作多型映射? Apr 17, 2024 pm 12:09 PM

Hibernate多態映射可映射繼承類別到資料庫,提供以下映射類型:joined-subclass:為子類別建立單獨表,包含父類別所有欄位。 table-per-class:為子類別建立單獨資料表,僅包含子類別特有列。 union-subclass:類似joined-subclass,但父類別表聯合所有子類別列。

在PHP中使用MySQLi建立資料庫連線的詳盡教學 在PHP中使用MySQLi建立資料庫連線的詳盡教學 Jun 04, 2024 pm 01:42 PM

如何在PHP中使用MySQLi建立資料庫連線:包含MySQLi擴充(require_once)建立連線函數(functionconnect_to_db)呼叫連線函數($conn=connect_to_db())執行查詢($result=$conn->query())關閉連線( $conn->close())

深入解析HTML如何讀取資料庫 深入解析HTML如何讀取資料庫 Apr 09, 2024 pm 12:36 PM

HTML無法直接讀取資料庫,但可以透過JavaScript和AJAX實作。其步驟包括建立資料庫連線、發送查詢、處理回應和更新頁面。本文提供了利用JavaScript、AJAX和PHP來從MySQL資料庫讀取資料的實戰範例,展示如何在HTML頁面中動態顯示查詢結果。此範例使用XMLHttpRequest建立資料庫連接,發送查詢並處理回應,從而將資料填入頁面元素中,實現了HTML讀取資料庫的功能。

如何在PHP中處理資料庫連線錯誤 如何在PHP中處理資料庫連線錯誤 Jun 05, 2024 pm 02:16 PM

PHP處理資料庫連線報錯,可以使用下列步驟:使用mysqli_connect_errno()取得錯誤代碼。使用mysqli_connect_error()取得錯誤訊息。透過擷取並記錄這些錯誤訊息,可以輕鬆識別並解決資料庫連接問題,確保應用程式的順暢運作。

PHP處理資料庫中文亂碼的技巧與實踐 PHP處理資料庫中文亂碼的技巧與實踐 Mar 27, 2024 pm 05:21 PM

PHP是一種廣泛應用於網站開發的後端程式語言,它具有強大的資料庫操作功能,常用於與MySQL等資料庫進行互動。然而,由於中文字元編碼的複雜性,在處理資料庫中文亂碼時常常會出現問題。本文將介紹PHP處理資料庫中文亂碼的技巧與實踐,包括常見的亂碼原因、解決方法和具體的程式碼範例。常見的亂碼原因資料庫字元集設定不正確:資料庫建立時需選擇正確的字元集,如utf8或u

See all articles