PHP Novice (11) _php Tutorial

Source: Internet
Author: User
Tags php3 file
Database links

PHP's greatest feature is the ability to operate a database in a particularly powerful way, with PHP providing support for a variety of databases.

With PHP you can easily connect to a database, request data and display it in your Web site, or even modify the data in your database. In this section, we mainly take the MySQL database, which is most used with PHP on the Internet, as an example, to introduce the operation functions of the MySQL database and the basic operation of the database.

In the MySQL database, there are two functions that we use to connect to a 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 on a different port, a colon and port number can be added to the host name. Function parameters can also be default, if not, the default hostname is "localhost", the user name is the database administrator, the default value is "root", the password is empty. After the connection to the database is successful, both functions can return a connection number and return a false value 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 use the MySQL link parameters, including the hostname, username and password as the parameters of mysql_connect (), and get the return value of $db, so that in the following statement, we can add the variable $db as a connection number to the MySQL database.
mysql_select_db ("MyDB", $db); Link the PHP program to the MYDB database so that the program's link to the database is complete.

10.1 A simple database Guestbook

After we have completed the link to the database, we can do 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 tools that manage MySQL database are phpmyadmin_2. 0.5 is already installed and works correctly.

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

1, launch the browser, open phpmyadmin_2. 0.5 of the administration Web interface.

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

Next, we will 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 (+), email char (+), job char (+), comments BLOB, PRI MARY KEY (ID));

Finally, the following guestbook program is carried over to your machine under the writable directory, and saved as guestbook.php3 file. As simple as this, you already have your own guestbook.

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

?>


<title><?php echo $page _title;?></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 " Add message

";

if ($total = = 0) {
Print " Nobody left a message right now.

"; }

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 " n ";
Print "
n ";
Print "n";
Print "
Name: $i 1n ";
Print "
Email:$i 2n ";
Print "
Job: $i 3n ";
Print "
Comment:n ";
Print "
$i 4n ";
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 ("");
}
}

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




Please fill in your message




}

/* 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
Your message: $name
Your name: $email
Your e-mail: $job
Your work:
"," 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);

?>

Thank you, for your message.

View Message



}

/* 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 word yet"; }

ElseIf ($number = = "1") {
$entry = "Current message number 1 people"; }

else {
$entry = "The number of messages $number people at present"; }

echo "


";
echo "

$entry
";
echo "

Please leave your message

";

if ($number > "") {
echo "

View Message

"; }
echo "

";
}
?>

All rights reserved: Boundless horizon



http://www.bkjia.com/PHPjc/316809.html www.bkjia.com true http://www.bkjia.com/PHPjc/316809.html techarticle Database link 10. PHP's biggest feature is the ability to manipulate databases in particular, and PHP provides support for a variety of databases. Through PHP you can easily connect to the database, the number of requests ...

  • 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.