What are some potential pitfalls to be aware of when using the mysql_query function in PHP?
One potential pitfall when using the mysql_query function in PHP is that it is deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. It is recommended to use MySQLi or PDO instead for database interactions to ensure compatibility and security.
// Using MySQLi to perform a query
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$query = "SELECT * FROM table";
$result = $mysqli->query($query);
if ($result) {
// Process the result
} else {
echo "Error: " . $mysqli->error;
}
$mysqli->close();