Are there any security considerations to keep in mind when querying values from a MySQL database in PHP?

When querying values from a MySQL database in PHP, it is important to sanitize user input to prevent SQL injection attacks. One way to do this is by using prepared statements with parameterized queries, which separate SQL code from user input. This helps to prevent malicious users from injecting SQL code into queries.

// Establish a connection to the MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check for connection errors
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Prepare a SQL statement with a parameterized query
$stmt = $mysqli->prepare("SELECT * FROM table WHERE column = ?");
$stmt->bind_param("s", $user_input);

// Sanitize user input
$user_input = mysqli_real_escape_string($mysqli, $_GET['input']);

// Execute the query
$stmt->execute();

// Process the results
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // Do something with the data
}

// Close the statement and connection
$stmt->close();
$mysqli->close();