Home  >  Article  >  Backend Development  >  PHP newbies on the road (11)_PHP tutorial

PHP newbies on the road (11)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:01:34817browse

Database link

10. The biggest feature of PHP is its extremely powerful ability to operate databases. PHP provides support for a variety of databases.

Through PHP you can easily connect to the database, request data and display it in your web site, and even modify the data in the database. In this section, we mainly take the MySQL database that is most commonly used with PHP on the Internet as an example to introduce the relevant MySQL database operating functions and basic database operations.

In the MySQL database, there are two functions we use to connect to the database, they are:
integer mysql_connect(string host,string user,string password);
integer mysql_pconnect(string host , string user, string password);
The mysql_connect function and the mysql_pconnect function are both connections to the MySQL database on the specified host. If the database is located on a different port, you can add a colon and port number after the host name. The parameters of the function can also be left blank by default. If the parameters are not filled in, the default host name is "localhost", the user name is the database administrator, the default value is "root", and the password is empty. After successfully connecting to the database, both functions can return a connection number. If the connection fails, a false value is returned. Let's take a look at the following statements:
$db=mysql_connect("localhost","user","password");
mysql_select_db("mydb",$db);
?>
Note:
$db=mysql_connect("localhost","user","password"); We use the mysql link parameters, including host name, user name and password as mysql_connect() parameters, and the return value is $db. In this way, in the following statement, we can use the variable $db as a connection number to connect to the mysql database.
mysql_select_db("mydb",$db); Link the PHP program to the mydb database, so that the link between the program and the database is completed.

10.1 A simple database guestbook

After completing the database link, we can perform a series of operations on the database. The following is a simple database guestbook program (guestbook.php3):

I assume that the MySQL database on your machine and the tool Phpmyadmin_2. 0.5 for managing the MYSQL database have been installed and can work normally.

The first thing we need to do is to create a message database, assuming the name is: mydb.

1. Start the browser and open the management WEB interface of Phpmyadmin_2. 0.5.

2. Enter the database name mydb in the "Create new database" text box, and then press the create button.

Next, we need to create a data table under the message database, assuming the name is: guestbook.

The command to create the data table is as follows:

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

Finally, download the following guestbook program to the writable directory of your machine and save it as guestbook.php3 file. It's that simple, you already have your own guestbook.  

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

/* $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";

?>


<?php 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 "

加入留言


";

if ($total== 0) {
print "
此刻没人留言


"; }

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 < 0) {
$offset=0; }

$counter=$offset-$lpp;

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

else {
$num=$total; } }

while ($counter < $num) {
$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:$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 < $total) {
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:


您的工作:


您的留言:



  
先观看所有的留言




}

/* 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) schreef het volgende bericht in het gastenboek :
$comments
您的留言:$name
您的大名:$email
您的email:$job
您的工作:
", "From: $name <$email>nReply-To: $name <$email>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);

?>

感谢, , 您的留言.

观看留言



}

/* 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 "

请您留言

";

if ($number > "") {
echo "

观看留言

"; }
echo "

";
}
?>

版权所有:无边天际



  

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/316809.htmlTechArticle数据库链接 10. PHP最大的特色就是操作数据库的能力特别的强大,PHP提供对多种数据库的支持。 通过PHP你可以轻松的连接到数据库,请求数...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn