Bài viết này sẽ hướng dẫn cách tải(load) dữ liệu từ database thông qua một ví dụ đơn giản. Trang php này sẽ hiển thị thông tin khách hàng từ database.
Đầu tiên ta cần lập một danh sách các công việc cần được thực hiện.
- Tạo bảng person trên database.
- Tạo trang Config.php
- Tạo trang LoadData.php
- Tạo trang LoadData.css
Đầu tiên ta tạo bảng person trên database như sau:
create table person
(
id int not null auto_increment,
name varchar(100) not null,
email varchar(100) not null,
phone varchar(10) not null,
address varchar(100) not null,
primary key(id)
)
(
id int not null auto_increment,
name varchar(100) not null,
email varchar(100) not null,
phone varchar(10) not null,
address varchar(100) not null,
primary key(id)
)
Tạo trang Config.php
<?php
// Replace the variable values below with your specific database information.
$host = "host";
$user = "user";
$pass = "pass";
$db = "database";
// This part sets up the connection to the database (so you don't need to reopen the connection again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}
// Then you need to make sure the database you want is selected.
mysql_select_db($db);
?>
// Replace the variable values below with your specific database information.
$host = "host";
$user = "user";
$pass = "pass";
$db = "database";
// This part sets up the connection to the database (so you don't need to reopen the connection again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}
// Then you need to make sure the database you want is selected.
mysql_select_db($db);
?>
Tạo trang LoadData.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="LoadData.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
include('Config.php');
$sql = "SELECT * FROM person";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<div class='data'>";
echo "<div>" . $row['name'] . "</div>";
echo "<div>" . $row['email'] . "</div>";
echo "<div>" . $row['phone'] . "</div>";
echo "<div>" . $row['address'] . "</div>";
echo "</div>";
}
?>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="LoadData.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
include('Config.php');
$sql = "SELECT * FROM person";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<div class='data'>";
echo "<div>" . $row['name'] . "</div>";
echo "<div>" . $row['email'] . "</div>";
echo "<div>" . $row['phone'] . "</div>";
echo "<div>" . $row['address'] . "</div>";
echo "</div>";
}
?>
</body>
</html>
Tạo trang LoadData.css
@charset "utf-8";
.data {
font-family: Verdana, Geneva, sans-serif;
border: 1px solid #333;
padding: 5px;
margin: 5px;
}
.data {
font-family: Verdana, Geneva, sans-serif;
border: 1px solid #333;
padding: 5px;
margin: 5px;
}
Bạn có thể download toàn bộ ví dụ của bài viết tại liên kết bên dưới.
No comments:
Post a Comment