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 generate a UUID in MySQL using PHP?
- How to create an SSL connection to MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to get the version of MySQL using PHP?
- How to get the last insert ID in PHP MySQL?
- How to join tables with PHP and MySQL?
- How to escape a string for MySQL in PHP?
- How to get the first row of a result in MySQL using PHP?
- How to change database in MySQL with PHP?
See more codes...