What are the potential pitfalls of using the mysql_fetch_assoc function in PHP when retrieving data from a MySQL database?
The potential pitfall of using the mysql_fetch_assoc function in PHP is that it is deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. To avoid this issue, you should use the mysqli_fetch_assoc function instead, which is the improved version of mysql_fetch_assoc.
<?php
// Connect to database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Query database
$result = $mysqli->query("SELECT * FROM table");
// Fetch data using mysqli_fetch_assoc
while ($row = $result->fetch_assoc()) {
// Process data
echo $row['column_name'] . "<br>";
}
// Close connection
$mysqli->close();
?>
Related Questions
- What are the recommended methods for preventing users from modifying data in PHP forms?
- Are there any recommended tutorials for creating a login system with sessions in PHP?
- How can PHP be used to automate the execution of tasks at specific intervals or times, such as every X minutes or daily at a specific time?