What are some best practices for handling and displaying data from a database in PHP?

When handling and displaying data from a database in PHP, it is important to sanitize user input to prevent SQL injection attacks. Additionally, it is recommended to use prepared statements to interact with the database to prevent SQL injection and improve performance. Finally, consider implementing pagination to display large datasets in a user-friendly manner.

// Sanitize user input to prevent SQL injection
$user_input = mysqli_real_escape_string($conn, $_POST['user_input']);

// Use prepared statements to interact with the database
$stmt = $conn->prepare("SELECT * FROM table WHERE column = ?");
$stmt->bind_param("s", $user_input);
$stmt->execute();
$result = $stmt->get_result();

// Implement pagination to display large datasets
$limit = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $limit;

$stmt = $conn->prepare("SELECT * FROM table LIMIT ?, ?");
$stmt->bind_param("ii", $offset, $limit);
$stmt->execute();
$result = $stmt->get_result();