Cách hiển thị tất cả dữ liệu từ cơ sở dữ liệu trong php?

Đế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]{
        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 đượ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ư sau

CHỌ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]{
        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, 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]{
        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 để lấy dữ liệu bảng

Xem 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]{
        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];
?>
9] 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

Tải xuống

 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]{
        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 được lưu trữ trong biến
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[];
?>
1. 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];
?>
9

Nế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
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[];
?>
1 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

Làm cách nào để hiển thị tất cả các bản ghi từ bảng trong PHP?

Điều này có thể được thực hiện với mã PHP sau. ?query["SELECT * FROM table_name"]; Toàn bộ nội dung .

Làm cách nào để lấy tất cả các hàng từ cơ sở dữ liệu trong PHP?

Hàm fetch_all[] / mysqli_fetch_all[] tìm nạp tất cả các hàng kết quả và trả về tập kết quả dưới dạng một mảng kết hợp, một mảng số .

Làm cách nào để lấy tất cả dữ liệu từ cơ sở dữ liệu trong PHP và hiển thị trong bảng?

php $connect=mysql_connect['localhost', 'root', 'mật khẩu'];

Làm cách nào để sử dụng tìm nạp tất cả trong PHP?

Sử dụng phương thức PHP fetchAll[] với phương thức query[] . Đầu tiên, thực hiện truy vấn bằng cách gọi phương thức query[] của đối tượng PDO. Sau đó, tìm nạp tất cả các hàng từ tập hợp kết quả vào một mảng bằng phương thức fetchAll[].

Chủ Đề