Có một vài phương pháp sử dụng mà bạn có thể sử dụng lấy dữ liệu từ cơ sở dữ liệu trong PHP và hiển thị nó vào bảng HTML. Trong hướng dẫn này, chúng tôi đã giải thích phương pháp sau để lấy dữ liệu từ cơ sở dữ liệu trong PHP và hiển thị nó trong bảng HTML
- Chương trình cơ bản sử dụng Loop
- Sử dụng Ajax
- Sử dụng bảng dữ liệu
1] Chương trình cơ bản sử dụng Loop
0]{
}else{
$msg = "No Record found";
}
}
?>
HTML and PHP code
Display user list using HTML and PHP
Name
Email
Status
Registrating Date
đầu ra
2] Sử dụng Ajax
Mã tệp HTML
HTML and PHP code
Display user list using HTML and PHP
Name
Email
Registrating Date
Mã tệp PHP
0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
echo json_encode[$result_array];
}
?>
đầu ra
3] Sử dụng Bảng dữ liệu
Mã tệp HTML
HTML and PHP code
Display user list using HTML and PHP
Name
Email
Registrating Date
Mã tệp PHP
0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
$results = ["sEcho" => 1,
"iTotalRecords" => count[$result_array],
"iTotalDisplayRecords" => count[$result_array],
"aaData" => $result_array ];
echo json_encode[$results];
}
?>
đầu ra
Đến đây bạn đã học cách tạo cơ sở dữ liệu và bảng cũng như chèn dữ liệu. Bây giờ là lúc lấy dữ liệu đã chèn trong hướng dẫn trước. Câu lệnh SQL
0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
echo json_encode[$result_array];
}
?>
1 được sử dụng để chọn các bản ghi từ các bảng cơ sở dữ liệu. Cú pháp cơ bản của nó như sauCHỌN cột1_name, cột2_name, cộtN_name TỪ tên_bảng;
Hãy tạo một truy vấn SQL bằng cách sử dụng câu lệnh
0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
echo json_encode[$result_array];
}
?>
1, sau đó chúng ta sẽ thực hiện truy vấn SQL này thông qua việc chuyển nó tới hàm PHP 0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
echo json_encode[$result_array];
}
?>
3 để lấy dữ liệu bảngXem xét bảng cơ sở dữ liệu người của chúng tôi có các bản ghi sau
+----+------------+-----------+----------------------+ | id | first_name | last_name | email | +----+------------+-----------+----------------------+ | 1 | Peter | Parker | peterparker@mail.com | | 2 | John | Rambo | johnrambo@mail.com | | 3 | Clark | Kent | clarkkent@mail.com | | 4 | John | Carter | johncarter@mail.com | | 5 | Harry | Potter | harrypotter@mail.com | +----+------------+-----------+----------------------+
Mã PHP trong ví dụ sau chọn tất cả dữ liệu được lưu trữ trong bảng người [sử dụng ký tự dấu hoa thị [
0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
echo json_encode[$result_array];
}
?>
4] thay cho tên cột sẽ chọn tất cả dữ liệu trong bảng]Thí dụ
PDO hướng đối tượng thủ tục
0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
connect_error];
}
// Attempt select query execution
$sql = "SELECT * FROM persons";
if[$result = $mysqli->query[$sql]]{
if[$result->num_rows > 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = $result->fetch_array[]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
$result->free[];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
$mysqli->close[];
?>
setAttribute[PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION];
} catch[PDOException $e]{
die["ERROR: Could not connect. " . $e->getMessage[]];
}
// Attempt select query execution
try{
$sql = "SELECT * FROM persons";
$result = $pdo->query[$sql];
if[$result->rowCount[] > 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = $result->fetch[]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
unset[$result];
} else{
echo "No records matching your query were found.";
}
} catch[PDOException $e]{
die["ERROR: Could not able to execute $sql. " . $e->getMessage[]];
}
// Close connection
unset[$pdo];
?>
Giải thích Mã [Kiểu thủ tục]
Trong ví dụ trên, dữ liệu trả về bởi hàm
0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
echo json_encode[$result_array];
}
?>
3 được lưu trữ trong biến 0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
echo json_encode[$result_array];
}
?>
6. Mỗi khi gọi 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
0, nó sẽ trả về hàng tiếp theo từ tập kết quả dưới dạng một mảng. Vòng lặp while được sử dụng để lặp qua tất cả các hàng trong tập kết quả. Cuối cùng, giá trị của trường riêng lẻ có thể được truy cập từ hàng bằng cách chuyển chỉ mục trường hoặc tên trường đến biến 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
1 như 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
2 hoặc 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
3, 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
4 hoặc 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
5, 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
6 hoặc 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
7 và 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
8 hoặc 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = mysqli_fetch_array[$result]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
mysqli_free_result[$result];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error[$link];
}
// Close connection
mysqli_close[$link];
?>
9Nếu bạn muốn sử dụng vòng lặp
connect_error];
}
// Attempt select query execution
$sql = "SELECT * FROM persons";
if[$result = $mysqli->query[$sql]]{
if[$result->num_rows > 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = $result->fetch_array[]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
$result->free[];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
$mysqli->close[];
?>
0, bạn có thể lấy giá trị bộ đếm vòng lặp hoặc số hàng mà truy vấn trả về bằng cách chuyển biến 0]{
$result_array = array[];
while[$row = mysqli_fetch_assoc[$result]]{
array_push[$result_array, $row];
}
}
echo json_encode[$result_array];
}
?>
6 cho hàm connect_error];
}
// Attempt select query execution
$sql = "SELECT * FROM persons";
if[$result = $mysqli->query[$sql]]{
if[$result->num_rows > 0]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
while[$row = $result->fetch_array[]]{
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
echo "idfirst_namelast_nameemail" . $row['id'] . "" . $row['first_name'] . "" . $row['last_name'] . "" . $row['email'] . "";
// Free result set
$result->free[];
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
$mysqli->close[];
?>
2. Giá trị bộ đếm vòng lặp này xác định số lần vòng lặp sẽ chạy