What is the potential issue with using mysql_fetch_assoc() in the provided PHP code?
The potential issue with using mysql_fetch_assoc() is that it is deprecated in newer versions of PHP and has been replaced by mysqli_fetch_assoc() or PDO. To solve this issue, you should update your code to use either mysqli or PDO for database operations instead of the deprecated mysql functions.
// Use mysqli or PDO instead of deprecated mysql functions
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while ($row = $result->fetch_assoc()) {
echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Related Questions
- What are some common mistakes to avoid when trying to style PHP output with background images?
- Are there any specific best practices for handling recursive data structures in PHP and MySQL when dealing with category hierarchies?
- What are common pitfalls when sending email attachments using PHP contact forms?