What potential issues could arise when trying to output data from a MySQL database using PHP?
One potential issue that could arise when outputting data from a MySQL database using PHP is the risk of SQL injection attacks if user input is not properly sanitized. To prevent this, always use prepared statements with parameterized queries to securely handle user input.
// Example of using prepared statements to prevent SQL injection
$mysqli = new mysqli("localhost", "username", "password", "database");
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$username = $_POST['username']; // Assuming user input is coming from a form
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// Output data from the database
}
$stmt->close();
$mysqli->close();