php-mysqlHow to get the first row of a result in MySQL using PHP?
To get the first row of a result in MySQL using PHP, you can use the mysqli_fetch_assoc()
function. This function takes a result set as an argument and returns an associative array of the first row.
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 of the first row.
Helpful links
More of Php Mysql
- How to join tables with PHP and MySQL?
- How to use a variable in a MySQL query using PHP?
- How to update to null value in MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to fetch data from MySQL in PHP?
- How to check if a record exists in PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to escape a string for MySQL in PHP?
- How to convert MySQL datetime to string in PHP?
- How to export data from MySQL to Excel using PHP?
See more codes...