What are the differences between using fetch_assoc and fetch_array in PHP sessions?
When fetching data from a MySQL database in PHP sessions, the main difference between using fetch_assoc and fetch_array is the format in which the data is returned. fetch_assoc returns an associative array where the keys are the column names, while fetch_array returns a numerical array with both numeric and associative keys. Depending on how you want to access the fetched data, you can choose the appropriate fetch method.
// Using fetch_assoc to fetch data from MySQL database in PHP session
$result = $mysqli->query("SELECT * FROM table");
while($row = $result->fetch_assoc()) {
// Access data using column names
echo $row['column_name'];
}
```
```php
// Using fetch_array to fetch data from MySQL database in PHP session
$result = $mysqli->query("SELECT * FROM table");
while($row = $result->fetch_array()) {
// Access data using both numeric and column names
echo $row['column_name'];
echo $row[0];
}