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 PHP be used to create custom URLs for different pages on a website?
- What best practices should be followed when starting XAMPP and other programs to avoid port conflicts?
- In the context of PHP programming, what are the differences between the UPDATE and INSERT INTO SQL commands for database manipulation?