9951 explained code solutions for 126 technologies


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

  1. mysqli_connect() - Establishes a connection to the MySQL database.
  2. mysqli_query() - Executes an SQL query on the database.
  3. mysqli_fetch_assoc() - Fetches a row from the result set as an associative array.

Helpful links

Edit this code on GitHub