Home Backend Development PHP Tutorial PHP connects to the database and dynamically displays database records_PHP tutorial

PHP connects to the database and dynamically displays database records_PHP tutorial

Jul 13, 2016 pm 05:16 PM
mysql php No introduce getting Started beginner dynamic Example database article show Record connect

This article will introduce a good introductory example of PHP mysql to PHP beginners. We connect to the database and display the record examples in the database. Friends can refer to it.

Connect to a MySQL database

Before you can access and process data in the database, you must create a connection to the database.

In PHP, this task is accomplished through the mysql_connect() function.

Grammar

mysql_connect(servername,username,password);

In the example below, we store the connection in a variable ($con) for later use in the script. If the connection fails, the "die" part will be executed:

// some code
The code is as follows
 代码如下 复制代码

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

// some code

?>

Copy code

 代码如下 复制代码


<?php
mysql_connect("localhost","root","");
mysql_select_db("cinema");
$_sql="select * from `t_hall`";
mysql_query("set names utf8");//编码设置为utf8
$query=mysql_query($_sql);
echo "<table align='center' border='1' cellspacing='0' width='550' >";
echo "<tr><td>影厅编号</td><td>大厅名</td><td>座位行</td><td>座位列</td></tr>";
while($row=mysql_fetch_array($query))
{
//从数据库查询出来的字段
//将数据放到html的表格中

echo "<tr onclick='GetText()'><td>{$row['HID']}</td><td>{$row['HHall']}</td><td>{$row['HSeatline']}</td><td>{$row['HSeatrow']}</td></tr>";
}
echo "</table>";
?>

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

?>

After having the above foundation, we can query the database and display it
The code is as follows Copy code

<?php
mysql_connect("localhost","root","");
mysql_select_db("cinema");
$_sql="select * from `t_hall`";
mysql_query("set names utf8");//Encoding is set to utf8
$query=mysql_query($_sql);
echo "<table align='center' border='1' cellspacing='0' width='550' >";
echo "<tr><td>A theater number</td><td>Hall name</td><td>Seat row</td><td>Seat row</td&gt ;</tr>";
while($row=mysql_fetch_array($query))
{
//Fields queried from the database
//Put the data into the html table echo "<tr onclick='GetText()'><td>{$row['HID']}</td><td>{$row['HHall']}< /td><td>{$row['HSeatline']}</td><td>{$row['HSeatrow']}</td></tr>";
}
echo "</table>";
?>
http://www.bkjia.com/PHPjc/628663.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628663.htmlTechArticleThis article will introduce a good php mysql entry example for php beginners. We connect to the database and display the database Friends can refer to the record examples in . Connect to a...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles