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 insert a date into a MySQL database using PHP?
- How to output XML from MySQL using PHP?
- How to run multiple queries in PHP and MySQL?
- How to use a variable in a MySQL query using PHP?
- How to use a MySQL union in PHP?
- How to set a timeout for MySQL query in PHP?
- How to create an SSL connection to MySQL using PHP?
- What port to connect to MySQL from PHP?
- How to use the LIKE operator in PHP and MySQL?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
See more codes...