How can the mysql_fetch_assoc() function be replaced when using MySQLi in PHP?
When using MySQLi in PHP, the mysql_fetch_assoc() function is replaced by the mysqli_fetch_assoc() function. This function fetches a row from a result set as an associative array. To use it, you need to first establish a connection to the database using the MySQLi functions and then execute a query to retrieve data. The mysqli_fetch_assoc() function can then be used to fetch each row of data as an associative array.
// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Execute a query
$result = $conn->query("SELECT * FROM table");
// Fetch data as an associative array
while ($row = $result->fetch_assoc()) {
// Process each row of data
}
// Close the connection
$conn->close();