How can the mysqli_fetch_assoc function be used in PHP code?
The mysqli_fetch_assoc function in PHP is used to fetch a single row from a result set returned by a query and return it as an associative array. To use this function, you need to first establish a connection to a MySQL database using the mysqli_connect function, execute a query using mysqli_query, and then fetch the result using mysqli_fetch_assoc.
// Establish a connection to the database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Execute a query
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);
// Fetch the result as an associative array
$row = mysqli_fetch_assoc($result);
// Access the data using keys
echo $row['column_name'];
Keywords
Related Questions
- How can PHP beginners ensure they are properly passing values of 0 or 1 for active or inactive states in a MySQL database?
- How can the microtime function in PHP be used to display milliseconds or microseconds in date output?
- In cases where a webpage consists mostly of tables, how can XPath be used in PHP to select and extract specific tables for processing?