How can PHP developers ensure the security and integrity of data when interacting with databases?

To ensure the security and integrity of data when interacting with databases in PHP, developers should use prepared statements with parameterized queries to prevent SQL injection attacks. Additionally, developers should validate and sanitize user input to prevent cross-site scripting attacks and other vulnerabilities.

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password');

// Prepare a SQL statement with placeholders
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');

// Bind parameters to the placeholders
$stmt->bindParam(':username', $_POST['username']);

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

// Fetch the results
$results = $stmt->fetchAll();