PHP Novice on the road (11) _php Foundation

Source: Internet
Author: User
Database links

PHP's biggest feature is the ability to manipulate the database is particularly powerful, PHP provides a variety of database support.

With PHP you can easily connect to the database, request the data and display it in your Web site, or even modify the data in the database. In this section we mainly in the Internet with PHP to use most of the MySQL database as an example, to introduce the relevant MySQL database operation functions and the basic operation of the database and other aspects of knowledge.

In the MySQL database, there are two functions we use to connect to the database, respectively:
Integer mysql_connect (string host,string user,string password);
Integer mysql_pconnect (string host,string user,string password);
Both the Mysql_connect function and the Mysql_pconnect function are connections to the MySQL database on the specified host, and if the database is located on a different port, you can add a colon and a port number after the host name. The parameters of the function can also be not filled out, if not fill in the parameters, the default host name is "localhost", the user name is the database administrator, the default value is "root", the password is empty. After a successful connection to the database, both functions can return a connection number and return a value of FALSE if the connection fails. Let's take a look at the following sentences:
?
$db =mysql_connect ("localhost", "User", "password");
mysql_select_db ("MyDB", $db);
?>
Comments:
$db =mysql_connect ("localhost", "User", "password"); We will MySQL's link parameters, including host name, username and password as mysql_connect () parameters, and get the return value of $db, so that in the following statement, we can use the variable $db as a connection to the MySQL database.
mysql_select_db ("MyDB", $db); Link the PHP program to the MYDB database so that the program's links to the database are complete.

10.1 A simple Database guest book

After the link to the database is complete, we can do a series of operations on the database. The following is a simple database guest Book program (GUESTBOOK.PHP3):

I assume you have the MySQL database on the machine and the tools to manage the MySQL database phpmyadmin_2. 0.5 are already installed and working properly.

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

1, start the browser, open phpmyadmin_2. 0.5 Management web interface.

2, enter the database name MyDB in the "Create New Database" text box, and press Create button.

Next, we will create a data table under the message database, assuming the name is: Guestbook.

The command to create the datasheet looks like this:

CREATE TABLE Guestbook (ID INT not NULL auto_increment, name char (), email char (), Job char (), Comments BLOB, PRI MARY KEY (ID));

Finally, take the following guest book program to the writable directory of your machine and save it as a guestbook.php3 file. As simple as that, you already have your own guest book.

10.2 Guest Book Program (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";

?>
<HTML>
<HEAD>
<title><?php echo $page _title;?></title>
</HEAD>
<body bgcolor= "#FFFFFF" link= "#000000" >
<font face= "Verdana" size= "-2" >
?

/* 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 "<center><font face=" Verdana "size="-2 "><a href=" Guestbook.php3?action=add "onmouseover=" Window.status= ' Add your name '; return true ' onmouseout= ' window.status= '; return true ' title= ' Add your name ' > Add message < /a></font></center><br><br> ";

if ($total = = 0) {
Print "<CENTER> no one at the moment message </CENTER><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 < 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 * *
$i 1=mysql_result ($result, $counter, "name");
$i 2=mysql_result ($result, $counter, "email");
$i 3=mysql_result ($result, $counter, "job");
$i 4=mysql_result ($result, $counter, "comments");

$i 4 = stripslashes ("$i 4");

/* Print it in a nice layout * *
print "<center>n";
Print "<table width=400 border=0 align=center valign=top><tr><td><font FACE=" Verdana "-2" >n ";
print "
Print "<BR><B>Name:</B> $i 1n";
Print "<br><b>email:</b><a href=" mailto: $i 2 "onmouseover=" window.status= ' email $i 2 '; return True "onmouseout=" window.status= "; return true" title= "Email $i 2" > $i 2</a>n ";
Print "<BR><B>Job:</B> $i 3n";
print "<br><b>comment:</b>n";
Print "<BR> $i 4n";
print "</font></td></tr></table>n";
print "</center>n";
$counter + +;
}
}
Mysql_close ();
}

/* Execute the function */
Search_it ($name);

/* If we need to put on the NEXT or PREVIOUS buttons *
if ($total > $LPP) {
Echo ("<form action=" $PHP _script "method=" POST ">n");

* * If we need a PREVIOUS button * *
if ($offset > $LPP) {
Echo ("<input type=" Submit "value=" Previous "Name=dir>n"); }

* * If we need a NEXT button * *
if ($offset < $total) {
Echo ("<input type=" Submit "value=" Next "Name=dir>n"); }

Echo ("<input type=hidden name=" offset "value=" $offset ">n");
Echo ("<input type=hidden name=" name "value=" $name ">n");
Echo ("</form>");
}
}

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

<table width= "460" align= "CENTER" valign= "Top" >
<th colspan= "2" ><P> please fill in the message </TH>
<form name= "Guestbook" action= "Guestbook.php3?action=send" method= "POST" >
<TR>
&LT;TD align= ' right ' valign= ' top ' >
Your name:</td>.
<td><input Type=text name=name></td>
</TR>
<TR>
&LT;TD align= ' right ' valign= ' top ' >
Your e-mail:</td>.
<TD>
<input Type=text name=email></td>
</TR>
<TR>
&LT;TD align= ' right ' valign= ' top ' >
Your work:</td>
<TD>
<input Type=text name=job></td>
</TR>
<TR>
&LT;TD align= ' right ' valign= ' top ' >
Your message:</td>
<TD>
<textarea name=comments cols=40 rows=6></textarea>
<P>
<input type=submit value=submit> <input type=reset value=reset>
<a align= "Right" href= "Guestbook.php3?action=view" onmouseover= "window.status= ' Read all comments a '; True "onmouseout=" window.status= '; return true "title=" Read All comments First "><font size="-2 "> Watch all Messages < /font></a>
</TD>
</TR>
</FORM>
</TABLE>
</CENTER>

?
}

/* 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", "
$comments = addslashes ("$comments");
$query = "INSERT into guestbook VALUES (', ' $name ', ' $email ', ' $job ', ' $comments ')";
$result = mysql_query ($query);

?>
<br><p ALIGN = center> Thanks, <?php echo $name, your message.
<br><p ALIGN = center><a href= "Guestbook.php3?action=view" onmouseover= "window.status=" View your Comment Now '; return true ' onmouseout= ' window.status= '; return true ' title= view your comment now > watch message </A> <BR><BR>
?

}

/* 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 = "No one has left a message"; }

ElseIf ($number = = "1") {
$entry = "The current number of 1 people"; }

else {
$entry = "Current number of people $number;" }

echo "<CENTER><BR>";
echo "<P> $entry <BR>";
echo "
if ($number > "") {
echo "echo "</P></CENTER>";
}
?>
<BR><SMALL><CENTER> All rights reserved: <a href= "http://personal.668.cc/haitang/index.htm" onmouseover= " window.status= ' pert '; return true "onmouseout=" window.status= "; return true" title= "pert" > Boundless Horizon </a></ Center></small>
</FONT>
</BODY>
</HTML>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.