php-mysqlHow to get a single value from query in PHP MySQL?
To get a single value from a query in PHP MySQL, you can use the mysqli_fetch_assoc()
function. This function takes a result set as an argument and returns an associative array with the first row of the result set.
Example code
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
echo $row['column_name'];
Output example
value
Code explanation
mysqli_query($conn, $sql)
: This function executes the SQL query and returns a result set.mysqli_fetch_assoc($result)
: This function takes a result set as an argument and returns an associative array with the first row of the result set.$row['column_name']
: This is used to access the value of a particular column in the result set.
Helpful links
More of Php Mysql
- How to use a variable in a MySQL query using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to get the first row of a result in MySQL using PHP?
- How to get the last insert ID in PHP MySQL?
- How to convert MySQL datetime to string in PHP?
- How to update to null value in MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to use a MySQL union in PHP?
- How to set a timeout for MySQL query in PHP?
- How to export data from MySQL to Excel using PHP?
See more codes...