Hướng dẫn dùng php fetch_assoc trong PHP

Ví dụ sau đây truy xuất các cột id, firstname và lastname từ bảng MyGuests và hiển thị nó trên trang:

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>

Đây là kết quả:

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley

Đầu tiên, chúng tôi tạo một truy vấn SQL truy xuất các cột id, firstname và lastname từ bảng MyGuests. Dòng mã tiếp theo sẽ thực thi truy vấn và lưu dữ liệu kết quả vào biến

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
4.

Sau đó, hàm

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
5 sẽ kiểm tra có dữ liệu trả về hay không.

Nếu có thì hàm

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
6 sẽ gán tất cả kết quả vào một mảng kết hợp mà chúng ta có thể duyệt qua. Vòng lặp
id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
7 sẽ duyệt qua tập kết quả rồi hiển thị các cột id, firstname và lastname lên trang.

Ví dụ sau đây tương tự như ví dụ trên, nhưng nó sử dụng MySQLi thủ tục:

 0) {
  // output data of each row
  while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } mysqli_close($conn); ?>

Đây là kết quả:

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley

Truy xuất dữ liệu với PDO

Ví dụ sau sử dụng PDO để truy xuất dữ liệu.

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
  $stmt->execute();

  // set the resulting array to associative
  $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
  foreach($stmt->fetchAll() as $row) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo ""; ?>

Đây là kết quả:

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley

Lọc dữ liệu từ cơ sở dữ liệu MySQL

Mệnh đề WHERE được sử dụng để lọc những bản ghi đáp ứng một điều kiện cụ thể.

SELECT column_name(s) 
FROM table_name 
WHERE column_name operator value 

Lọc dữ liệu với MySQLi

Ví dụ sau đây truy xuất các cột id, firstname và lastname từ bảng MyGuests những người có họ là "Doe" và hiển thị nó trên trang:

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests WHERE lastname='Doe'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>

Đây là kết quả:

id: 1 - Name: John Doe

Đầu tiên, chúng tôi tạo truy vấn SQL truy xuất các cột id, firstname và lastname từ bảng MyGuests những người có họ là "Doe". Dòng mã tiếp theo sẽ thực thi truy vấn và lưu dữ liệu kết quả vào biến

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
4.

Sau đó, hàm

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
5 sẽ kiểm tra có dữ liệu trả về hay không.

Nếu có thì hàm

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
6 sẽ gán tất cả kết quả vào một mảng kết hợp mà chúng ta có thể duyệt qua. Vòng lặp
id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
7 sẽ duyệt qua tập kết quả rồi hiển thị các cột id, firstname và lastname lên trang.

Ví dụ sau đây tương tự như ví dụ trên, nhưng nó sử dụng MySQLi thủ tục:

 0) {
  // output data of each row
  while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } mysqli_close($conn); ?>

Đây là kết quả:

id: 1 - Name: John Doe

Lọc dữ liệu với PDO

Ví dụ sau sử dụng PDO để truy xuất dữ liệu.

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>
2

Đây là kết quả:

id: 1 - Name: John Doe

Sắp xếp dữ liệu từ cơ sở dữ liệu MySQL

Câu lệnh ORDER BY được sử dụng để sắp xếp các bản ghi theo thứ tự tăng dần (ASC) theo mặc định. Để sắp xếp các bản ghi theo thứ tự giảm dần, hãy sử dụng từ khóa DESC.

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>
4

Sắp xếp dữ liệu với MySQLi

Ví dụ sau đây truy xuất các cột id, firstname và lastname từ bảng MyGuests theo thứ tự lastname tăng dần và hiển thị nó trên trang:

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>
5

Đây là kết quả:

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>
6

Đầu tiên, chúng tôi tạo một truy vấn SQL truy xuất các cột id, firstname và lastname từ bảng MyGuests theo thứ tự lastname tăng dần. Dòng mã tiếp theo sẽ thực thi truy vấn và lưu dữ liệu kết quả vào biến

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
4.

Sau đó, hàm

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
5 sẽ kiểm tra có dữ liệu trả về hay không.

Nếu có thì hàm

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
6 sẽ gán tất cả kết quả vào một mảng kết hợp mà chúng ta có thể duyệt qua. Vòng lặp
id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
7 sẽ duyệt qua tập kết quả rồi hiển thị các cột id, firstname và lastname lên trang.

Ví dụ sau đây tương tự như ví dụ trên, nhưng nó sử dụng MySQLi thủ tục:

 0) {
  // output data of each row
  while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } mysqli_close($conn); ?>

Đây là kết quả:

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>
6

Sắp xếp dữ liệu với PDO

Ví dụ sau sử dụng PDO để truy xuất dữ liệu.

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>
9

Đây là kết quả:

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
"; } } else { echo "0 results"; } $conn->close(); ?>
6

Giới hạn truy xuất dữ liệu từ cơ sở dữ liệu MySQL

MySQL cung cấp mệnh đề LIMIT để chỉ định số lượng bản ghi sẽ trả về.

Mệnh đề LIMIT giúp dễ dàng giới hạn kết quả trả về hoặc phân trang bằng SQL và rất hữu ích trên các bảng lớn. Trả về quá nhiều dữ liệu trong một lần truy vấn có thể ảnh hưởng đến hiệu suất của hệ thống.

Giả sử chúng tôi muốn truy xuất tất cả các bản ghi từ 1 - 30 từ một bảng MyGuests. Truy vấn SQL sẽ trông như thế này:

id: 1 - Name: John Doe
id: 2 - Name: Mary Moe
id: 3 - Name: Julie Dooley
1

Khi chạy truy vấn SQL ở trên nó sẽ trả về 30 bản ghi đầu tiên.

Nếu chúng tôi muốn truy xuất 10 bản ghi tính từ bản ghi thứ 16 thì sao?

Mysql cũng cung cấp một cách để xử lý việc này đó là sử dụng từ khóa OFFSET. Ví dụ dưới đây minh họa sử dụng từ khóa OFFSET: