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 change database in MySQL with PHP?
- How to get the version of MySQL using PHP?
- How to create an SSL connection to MySQL using PHP?
- How to update to null value in MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to check the result of an insert in PHP and MySQL?
- How to use MySQL transactions in PHP?
- How to get a single value from query in PHP MySQL?
- How to get table column names in PHP MySQL?
See more codes...