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 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 use a MySQL union in PHP?
- How to generate a UUID in MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to convert a MySQL timestamp to a datetime in PHP?
- How to use MySQL transactions in PHP?
- How to change database in MySQL with PHP?
- How to create an SSL connection to MySQL using PHP?
See more codes...