php-mysqlHow to fetch data from MySQL in PHP?
Fetching data from MySQL in PHP can be done using the mysqli_query()
function. This function takes two parameters, the first being the connection to the MySQL database, and the second being the SQL query.
$conn = mysqli_connect("localhost", "username", "password", "database");
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);
The output of the above code will be a mysqli_result
object, which can be used to loop through the results of the query.
while ($row = mysqli_fetch_assoc($result)) {
echo $row['column_name'];
}
Code explanation
mysqli_connect()
- Establishes a connection to the MySQL database.mysqli_query()
- Executes an SQL query on the database.mysqli_fetch_assoc()
- Fetches a row from the result set as an associative array.
Helpful links
More of Php Mysql
- How to get the version of MySQL using PHP?
- How to convert a MySQL timestamp to a datetime in PHP?
- How to update to null value in MySQL using PHP?
- How to get the last insert ID in PHP MySQL?
- How to generate a UUID in MySQL using PHP?
- How to replace a string in MySQL using PHP?
- How to order by a column in ascending order in MySQL using PHP?
- How to run multiple queries in PHP and MySQL?
- How to get table column names in PHP MySQL?
See more codes...