What are the advantages and disadvantages of using AJAX to display content from a database in PHP?
Issue: When displaying content from a database in PHP, using AJAX can provide a smoother user experience by loading content dynamically without refreshing the entire page. However, it can also add complexity to the code and may require additional server-side handling to properly retrieve and display the data.
<?php
// PHP code to retrieve data from database and return as JSON
// This code snippet assumes you have a database connection established
// Retrieve data from database
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);
// Fetch data as an associative array
$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
// Return data as JSON
echo json_encode($rows);
?>
Keywords
Related Questions
- What are some best practices for using preg_match in PHP to find multiple words in a text?
- How should database credentials be securely stored and utilized in PHP scripts?
- How can server-side scripting languages like PHP be integrated with client-side scripting languages like JavaScript for efficient web development?