Home > Article > Backend Development > PHP and Mysql connection_PHP tutorial
After logging in, it will look like this:
First we create a database for testing.
If you are lazy, just create it directly in phpMyAdmin.
I created a books database with an item table in it:
Next, implement a function: connect to the database and print out the contents of the table.
Connect to database
[php]
$db_host='localhost';
$db_user='root';
$db_pass='1';
$db_name='books';
$conn=mysql_connect($db_host,$db_user,$db_pass)
mysql_select_db($db_name,$conn);
mysql_query("set names 'utf8'");
if(!$conn) echo "failed!";
else echo "success!";
?>
$db_host='localhost';
$db_user='root';
$db_pass='1';
$db_name='books';
$conn=mysql_connect($db_host,$db_user,$db_pass)
mysql_select_db($db_name,$conn);
mysql_query("set names 'utf8'");
if(!$conn) echo "failed!";
else echo "success!";
?>
If the connection is normal, the browser will display seccess!
Create list.php
[php]
include("conn.php");
$sql="select * from items";
$query=mysql_query($sql);
echo "
Id | Name |
".$id." | ".$name." |
include("conn.php");
$sql="select * from items";
$query=mysql_query($sql);
echo "
Id | Name |
".$id." | ".$name." |
Finish and call it a day.