Connect PHP to a MySQL database instance
For those familiar with websites, if they want to make websites dynamic, they must have database support and use specific scripts to connect to the database, extracts resource H from the database, adds data to the database, and deletes data. Here I use an example to illustrate how to use PHP to connect to the MySQL database.
I am going to create a simple address book. The database name is txl. The database has only one table named personal_info, and the table has five fields.
Pi_id pi_name pi_tel pi_qq pi_email
First, create a database:
Create database txl;
Then we create a table.
Create table 'personal _ info '(
'Pi _ id' bigint (20) not null auto_increment,
'Pi _ name' varchar (50) not null,
'Pi _ tel 'varchar (15) default NULL,
'Pi _ qq' varchar (15) default NULL,
'Pi _ email 'varchar (50) default NULL,
Primary key ('Pi _ id ')
) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 4;
The preceding SQL statement is simple and can be guessed literally.
Below are all the fields connecting to the database and displaying the table personal_info:
// Connsql. php
<? Php
$ Mysql_server_name = "localhost"; // database server name
$ Mysql_username = "root"; // user name used to connect to the database
$ Mysql_password = "root"; // connection password
$ Mysql_database = "lxr"; // Database Name
// Connect to the database
$ Conn = mysql_connect ($ mysql_server_name, $ mysql_username,
$ Mysql_password );
// SQL statement used to extract information from the table
$ Strsql = "select * from personal_info ";
// Execute SQL query
$ Result = mysql_db_query ($ mysql_database, $ strsql, $ conn );
// Obtain the query result
$ Row = mysql_fetch_row ($ result );
Echo '<font face = "verdana"> ';
Echo '<table border = "1" cellpadding = "1" cellspacing = "2"> ';
// Display the field name
Echo "/n <tr>/n ";
For ($ I = 0; $ I <mysql_num_fields ($ result); $ I ++)
{
Echo '<td bgcolor = "#000F00"> <B> '.
Mysql_field_name ($ result, $ I );
Echo "</B> </td>/n ";
}
Echo "</tr>/n ";
// Locate the first record
Mysql_data_seek ($ result, 0 );
// How can I query the records retrieved cyclically?
While ($ row = mysql_fetch_row ($ result ))
{
Echo "<tr>/n ";
For ($ I = 0; $ I <mysql_num_fields ($ result); $ I ++)
{
Echo '<td bgcolor = "#00FF00"> ';
Echo "$ row [$ I]";
Echo '</td> ';
}
Echo "</tr>/n ";
}
Echo "</table>/n ";
Echo "</font> ";
// Release the resource hovertree.com
Mysql_free_result ($ result );
// Close the connection
Mysql_close ();
?>
The running result is as follows:
Pi_id pi_name pi_tel pi_qq pi_email
1 Zhangsan 13911111111 642864125 bkjia@bkjia.com
2 Lisi 13122222222 63958741 lisi@bkjia.com
3 Wangwu 13833333333 912345678 wangwu@bkjia.com
The so-called "changing things without changing things", and complex operations are based on the above, all of which are based on the above basic steps. When necessary, check the relevant manual to solve the problem.
Reference code:
<H1> insert operation <? Php
If (! Isset ($ _ POST ['submit ']) {
// If no form is submitted, a form is displayed.
?>
<Form action = "" method = "post">
Country: <input type = "text" name = "country"/>
Animal name (English): <input type = "text" name = "animal"/>
Animal name (Chinese): <input type = "text" name = "cname"/>
<Input type = "submit" name = "submit" value = "submit Form"/>
</Form>
<? Php
}
Else
{
// If the form hovertree.com is submitted
// Database connection Parameters
$ Host = "localhost ";
$ User = "root ";
$ Pass = "zq19890319 ";
$ Db = "phpdev ";
// Obtain the value in the form, check whether the value in the form complies with the standard, and escape it to prevent SQL injection.
$ Country = empty ($ _ POST ['country'])? Die ("enter the country name "):
Mysql_escape_string ($ _ POST ['country']);
$ Animal = empty ($ _ POST ['animal '])? Die ("Enter English name "):
Mysql_escape_string ($ _ POST ['animal ']);
$ Cname = empty ($ _ POST ['cname'])? Die ("enter Chinese name "):
Mysql_escape_string ($ _ POST ['cname']);
// What should I ask when I open the database connection?
$ Connection = mysql_connect ($ host, $ user, $ pass) or die ("Unable to connect! ");
// Select a database
Mysql_select_db ($ db) or die ("Unable to select database! ");
// Construct an SQL query
$ Query = "insert into symbols (country, animal, cname) VALUE ('$ country', '$ animal', '$ cname ')";
// Execute this query
$ Result = mysql_query ($ query) or die ("Error in query: $ query.". mysql_error ());
// The insert record number is displayed after the insert operation is successful.
Echo "the record has been inserted, mysql_insert_id () =". mysql_insert_id ();
// Close the current database connection
Mysql_close ($ connection );
}
?>
This article permanently updates the link address: