Home > Article > Backend Development > PHP newbies on the road (11)_PHP tutorial
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";
?>
n"; print " n"; print " Name: $i1n"; print " email:$i2n"; print " Job: $i3n"; print " Comment:n"; print " $i4n"; print " |
请您填写留言 |
---|
$comments | |
您的留言: | $name |
您的大名: | |
您的email: | $job |
您的工作: |
感谢, , 您的留言.
观看留言
}
/* 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 "
$entry
";
echo "