PHP Beginners Basics (10)

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags basic basics beginners button comment connect counter create

PHP connection MySQL Database


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 database basic operation 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 link parameters, including host name, username and password as mysql_connect () parameters, and get the return value of WISHDB, so that in the following statement, we can use the variable wishdb 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 a MySQL database on the machine and a tool 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 datasheet 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, PRIMARY 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 dour 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 "<hr>n";
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/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>
<td align= " Right ' valign= top >
Your name:</td>
<td><input type=text name=name></td>
</tr >
<TR>
<td align= "right" valign= "top" >
your e-mail:</td>
<TD>
<input Type=text name=email></td>
</TR>
<TR>
<td align= ' right ' valign= ' top ' >
Your work :</td>
<TD>
<input type=text name=job></td>
</TR>
<TR>
<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 '; return 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 "," <html><body><font face= "Century Gothic" ><table BORDER= "0 "Width=" 100% "cellspacing=" 4 "><TR> $name ($email) schreef het volgende bericht in het gastenboek:</tr> <tr><td align= ' left ' > </td><td align= ' left ' nowrap> Align= "Left" > $comments </td><td align= "left" nowrap> </td></tr><tr><td ALIGN= " Left ' > </td><td align= ' left ' nowrap> </td></tr><tr><td align= ' left ' > your message: </TD><TD align= "left" nowrap> $name </td></tr><tr><td align= "left" > Your name: </TD ><TD align= "left" nowrap> $email </td></tr><tr><td align= ' left ' > your email:</td ><TD align= "left" nowrap> $job </TD></TR><TR><TD align= "Left" > Your work:</td></tr></table></body></font></html> "," From: $ Name < $email >nreply-to: $name < $email >ncontent-type:text/htmlnx-mailer:php/". Phpversion ());
}


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

?>
<br><p ALIGN = center> Thank you, <?php Echo $name;
<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 moment-in 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 "<h4><font face=" Verdana "size=" 3 "><a href=" Guestbook.php3?action=add "onmouseover=" Window.status= ' please leave a message '; return true "onmouseout=" window.status= ". return true" title= "Add your name to our Guestbook" > Please message </A></FONT></H4> ";

If ($number > "") {
echo "<h4><font face=" Verdana "size=" 3 "><a href=" guestbook.php3?action= View "onmouseover=" window.status= ' watch message '; return true ' onmouseout= ' window.status= '; return true ' title= ' view the names In our Guestbook "> Watch message </A></FONT></H4>";
Echo </P></CENTER>;
}
?>
<BR><SMALL><CENTER> Copyright: <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>

Related Article

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.