What are the potential pitfalls of using mysql_fetch_array to create an associative array in PHP?
Using mysql_fetch_array to create an associative array in PHP can lead to potential pitfalls because it returns both numeric and associative keys for each row fetched from the database, which can cause unexpected behavior when accessing the data. To avoid this issue, it is recommended to use mysql_fetch_assoc instead, which only returns associative keys for each row.
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Query the database
$query = "SELECT * FROM table";
$result = mysqli_query($conn, $query);
// Fetch rows as an associative array
while ($row = mysqli_fetch_assoc($result)) {
// Access data using associative keys only
echo $row['column_name'];
}
// Close the connection
mysqli_close($conn);