Select data from MySQL using PHP

How to select data from MySQL using PHP?

You can retrieve data from with select query. Select query is very powerful query you can retrieve data as you want. The syntax of select query is mentioned below: Syntax


SELECT column_name(s)
FROM table_name
If you want to retrieve all the columns then you can also use * sign in place of column_name(s). The example is mentioned below:

Code

<?php
  // Establish database connection
  $connection = mysqli_connect("localhost", "marcus", "xyz123", "my_database");

  // Check connection
  if (!$connection)
  {
    die("Could not connect with MySQL: " . mysqli_connect_error());
  }

  // Execute SELECT query
  $result = mysqli_query($connection, "SELECT * FROM Employee");

  // Check query execution
  if (!$result)
  {
    die("Error occurred: " . mysqli_error($connection));
  }

  // Fetch and display records
  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
  {
    print($row["FirstName"] . " " . $row["LastName"] . " " . $row["PhoneNo"]);
    print("
"); } // Free result memory mysqli_free_result($result); // Close database connection mysqli_close($connection); ?>
In the above mentioned example we have store all the data returned by the select query in the $result variable. Through this $result variable all the data or records retrieved in array format with the help of mysqli_fetch_array() built in function. mysqli_fetch_array() function returns one record at a time in array format. We retrieved all the records through while loop and prints it on the webpage. The output of the above mentioned example describe below:

Output

Edge    Boucher    35457845
Kane    Johnson    33457845
You can retrieve data from a table with select query and put it in HTML table. The example is mentioned below:

Code

<?php
  $connection = mysqli_connect("localhost","marcus","xyz123");
  if (!$conection)
  {
    die("Could not connect with MySQL: " . mysqli_connect_error());
  }
  
  print("");
  print("");
  print("");
  print("");
  print("");
  print("");
  mysqli_select_db($connection, "my_database");
  $result = mysqli_query($connection, "SELECT * FROM Employee");
  while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
  {
    print("");
    print("");
    print("");
    print("");
    print("");
  }
  print("
First NameLast NamePhone No
".$row["FirstName"] . "" . $row["LastName"] . "" . $row["PhoneNo"]."
"); mysqli_close($connection); ?>
The output of the code above will be:

Output

First Name    Last Name    Phone No
Edge          Boucher      989898989
Kane          Johnson      989898989

FAQ 1. How to prevent SQL injection in PHP MySQL SELECT queries? Use MySQLi prepared statements with parameter binding when retrieving records based on user-provided input. This separates SQL statements from input values and helps prevent SQL injection attacks. 2. How to display MySQL database records in an HTML table using PHP? You can execute a SELECT query using mysqli_query() and iterate through the returned records using a while loop. Inside the loop, generate HTML table rows using the retrieved database values. 3. Which PHP function is used to retrieve data from MySQL? The mysqli_query() function executes a SELECT query, while mysqli_fetch_array() and mysqli_fetch_assoc() are used to fetch the returned records.