php-mysqlHow to fetch an associative array from MySQL in PHP?
Fetching an associative array from MySQL in PHP can be done using the mysqli_fetch_assoc() function. This function takes a result set as an argument and returns an associative array.
Example code
$result = mysqli_query($conn, "SELECT * FROM table");
$row = mysqli_fetch_assoc($result);
Output example
Array
(
[column1] => value1
[column2] => value2
[column3] => value3
)
Code explanation
mysqli_query($conn, "SELECT * FROM table"): This line executes a query on the MySQL database and returns a result set.mysqli_fetch_assoc($result): This line takes the result set as an argument and returns an associative array.
Helpful links
More of Php Mysql
- How to use a MySQL union in PHP?
- How to get the last insert ID in PHP MySQL?
- How to convert a MySQL timestamp to a datetime in PHP?
- How to export data from MySQL to Excel using PHP?
- How to join tables with PHP and MySQL?
- How to replace a string in MySQL using PHP?
- How to get table column names in PHP MySQL?
- How to use MySQL transactions in PHP?
- Inserting data to MySQL using PHP
- How to return multiple rows as an array in MySQL using PHP?
See more codes...