11th Section Database link _php

Source: Internet
Author: User
Keywords link database 11 The align if name a
database links

PHP 's greatest feature is the ability to operate a database in a particularly powerful way, withPHP 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.

InMySQLIn the 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);
mysql_connect Functions andMysql_pconnectfunction is on the specified hostMySQLDatabase, if the database is on a different port, you can add a colon and a port number to the host name. The parameters of the function can also be not filled in by default, if the parameter is not filled, the default host name 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, if the connection fails, returns afalseValue. 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 willMySQLThe link parameters, including the hostname, user name, and password asmysql_connect()parameter, while getting the return value of$db, so that in the following statement, we can add the variable$dbAs a connectionMySQLThe connection number of the database to use.
mysql_select_db ("mydb", $db); WillPHPProgram linked toMyDBDatabase, the link between the program and 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 for managing mysql database phpmyadmin_ 2.0.5 are already installed and working correctly.

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

1 , launch Browser, open phpmyadmin_ 2.0.5 's administration WEB interface.

2 , enter the database name mydbin thecreate newdatabase 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):

!--? PHP
/* $host: Your mysql -host, usually ' localhost ' */
/* $user: Your mysql-username */
/* $password: Your mysql -password */
/* $data Base: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_nam E : 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=grame>?<span class=spelle>php</span></span> Echo $<SPAN class=SpellE> page_title</span>;?>



/* 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 "> Add message < BR>< BR> ";

if ($total = = 0) {
Print "

Nobody left a message right now. < 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 "

n ";
Print "
n ";
Print "n";
Print "
Name: $i 1n ";
Print "
Email:onmouseover= "window.status= ' email $i 2 '; return true" onmouseout= " Window.status= "; return true" title= "Email $i 2" > $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", "

Schreef het Volgende Bericht het Gastenboek $name ($email) in:
$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);

?>

Thanks , <? PHP echo $name;?, your message .

? action=view " onMouseOver="window.status= ' View your comment now '; return true " onmouseout= "window.status="; return true "title=" View your comment now "> 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);
= Mysql_numrows ($result);

if (= = "") {
$entry = " no one has left a word yet ";}

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

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

echo "


";
echo "

$entry
";
echo "

Onmouseover= "window.status= ' please leave a message '; return true" onmouseout= "window.status="; return true "title=" Add your name to US guestbook "> please leave a message

";

if (> "") {
echo "

Onmouseover= "window.status= ' watch message '; return true" onmouseout= "window.status = "; return true" title= "view the names in US Guestbook" > view message

"; }
echo "

";
}
?>

All rights reserved: onMouseOver= " window.status= ' pert '; return true" onmouseout= " win Dow.status= "; return true" title= "pert" > Infinity Horizon


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