What are the advantages of using mysqli_* or PDO functions over deprecated mysql_* functions in PHP for database queries?
The advantages of using mysqli_* or PDO functions over deprecated mysql_* functions in PHP for database queries include improved security, support for prepared statements to prevent SQL injection attacks, and better error handling capabilities.
// Using mysqli_* functions
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$result = $mysqli->query("SELECT * FROM table");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// Process each row
}
} else {
echo "0 results";
}
$mysqli->close();