Home  >  Article  >  Backend Development  >  Detailed explanation of how to connect to mysql instance with php_PHP tutorial

Detailed explanation of how to connect to mysql instance with php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:451029browse

Detailed explanation of how to use php to connect to mysql instance

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
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 '

';

// Display field name
echo "n

n";
for ($i=0; $i {
echo '
n";
}
echo "
n";
// Locate the first record
Mysql_data_seek($result, 0);
// Loop out records
While ($row=mysql_fetch_row($result))
{
echo "
n";
for ($i=0; $i           {
echo '
';
}
echo "
n";
}
 
echo "
'.
Mysql_field_name($result, $i);
echo "
';
           echo "$row[$i]";
echo '
n";
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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/950330.htmlTechArticleHow to use php to connect mysql instance to explain a simple address book, the name of the database is txl, and the database has only one table called personal_info, there are 5 fields in the table pi_idpi_namepi_telpi_qqpi_e...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn