Home > Backend Development > PHP Tutorial > PHP and Mysql connection_PHP tutorial

PHP and Mysql connection_PHP tutorial

WBOY
Release: 2016-07-14 10:09:48
Original
856 people have browsed it

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 "

";
while($row = mysql_fetch_array($query))
{
         $id=$row["id"];
         $name=$row["name"];
echo "";
}
echo "
IdName
".$id."".$name."
";
mysql_close();
?>

          include("conn.php");
          $sql="select * from items";
          $query=mysql_query($sql);
echo "

";
while($row = mysql_fetch_array($query))
{
         $id=$row["id"];
          $name=$row["name"];
echo "";
}
echo "
IdName
".$id."".$name."
";
mysql_close();
?>


Finish and call it a day.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477590.htmlTechArticleAfter logging in, it looks like this: First, let's create a database for testing. If you are lazy, just create it directly in phpMyAdmin. I created a books database with...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template