Home >Backend Development >PHP Tutorial >Detailed explanation of how to connect to mysql instance with php_PHP tutorial
A simple address book, the name of the database is txl, the database has only one table called personal_info, and there are 5 fields in the table
pi_idpi_namepi_telpi_qqpi_email // Display field name
First we need to create the database:
create database txl;
Then we create the 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 above SQL statement is very simple, and you can guess its meaning just by reading the words.
The following is connected to the database and displays all field information of the table personal_info:
// connsql.php
$mysql_server_name="localhost"; //Database server name
$mysql_username="root"; // Username to connect to the database
$mysql_password="root"; // Password to connect to the database
$mysql_database="lxr"; // Database name
// Connect to database
$conn=mysql_connect($mysql_server_name, $mysql_username,
$mysql_password);
// sql statement to extract information from the table
$strsql="select * from personal_info";
//Execute sql query
$result=mysql_db_query($mysql_database, $strsql, $conn);
// Get query results
$row=mysql_fetch_row($result);
echo '';
echo '';
n";
echo "nn"; n";
for ($i=0; $i
echo ''. n";
Mysql_field_name($result, $i);
echo "
}
echo "
// Locate the first record
Mysql_data_seek($result, 0);
// Loop out records
While ($row=mysql_fetch_row($result))
{
echo "n"; n";
for ($i=0; $i
echo ''; ';
echo "$row[$i]";
echo '
}
echo "
}
echo "
echo "";
// Release resources
Mysql_free_result($result);
//Close connection
Mysql_close();
?>
The following is the running result:
pi_idpi_namepi_telpi_qqpi_email1Zhangsan13911111111642864125zhangsan@126.com2Lisi1312222222263958741lisi
@163.com3Wangwu13833333333912345678wangwu@sohu.com
As the saying goes, "everything can change without departing from its roots." No matter how complicated the operation is, it is based on the above, and it is indispensable for the above basic steps. When necessary, you can check the relevant manuals to solve the problem.