What are the potential pitfalls of using deprecated functions like mysql_query in PHP for database operations?
Using deprecated functions like mysql_query in PHP for database operations can lead to security vulnerabilities and compatibility issues with newer versions of PHP. It is recommended to use modern alternatives like PDO or MySQLi for safer and more reliable database interactions.
// Using MySQLi for database operations
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$sql = "SELECT * FROM table";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
}
} else {
echo "0 results";
}
$mysqli->close();
Related Questions
- How can syntax highlighting tools affect the readability and usability of PHP code using <<<EOT syntax?
- What are the potential pitfalls of using imagefilledrectangle in PHP, especially when drawing shapes?
- What are the potential pitfalls of not using isset() or empty() functions in PHP form handling?