All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Code To Retrieve Data From MySQL Database And Display

Last Updated : Mar 11, 2024

PHP Code To Retrieve Data From MySQL Database And Display

In this article we will show you the solution of PHP code to retrieve data from MySQL database and display, Databases are collections of tables that store data. Using MySQL's "Select" query in PHP is the easiest way to retrieve data from MySQL databases.

SQL SELECT statements can be executed through the PHP function mysql_query to fetch data from MySQL tables.

MySQL gives you a number of options for retrieving data. MySQL_fetch_array () is the most common function used. Associative, numerical, or both are returned by this function. If no more rows are present, this function returns FALSE.

Step By Step Guide On PHP Code To Retrieve Data From MySQL Database And Display :-

<?php
$servername = "localhost";
$username = "root";
$password = "";
$databasename = "TalkersCode";
// CREATE A CONNECTION
$conn = new mysqli ($servername,
      $username, $password, $databasename);
// GET CONNECTION ERRORS
if ($conn->connect_error) {
      die ("Connection failed: ". $conn->connect_error);
}
// SQL QUERY
$query = "SELECT * FROM Student Details;";
// FETCHING DATA FROM THE DATABASE
$result = $conn->query($query);
      if ($result->num_rows > 0)
      {
                  // OUTPUT DATA OF EACH ROW
                  while ($row = $result->fetch_assoc())
                  {
                              echo "Roll No: ".
                                          $row["Roll_No"]. " - Name: ".
                                          $row["Name"]. " | City: ".
                                          $row["City"]. " | Age: ".
                                          $row["Age"]. "<br>";
                  }
      }
      else {
                  echo "0 results";
      }
$conn->close ();
?>
  1. The short tags start with "<?" and end with "?>". Short style tags are only available when they are enabled in the php.ini configuration file on servers.
  2. These validations will check if the fields are not empty or null. For the username, upon submitting the registration form the system will check if the value entered in the username field is a valid username.
  3. Then for the Password, the system will check if the entered characters' length in this field is at least 8. Lastly, the Confirm Password will check if the entered value in Password Field is identical equals to the value of this field.
  4. $conn is used for creating a connection.
  5. Then we use $query for creating SQL query.
  6. After that we fetch the database from the query.
  7. Then we find the output of the database of each row.
  8. The echo () function is not actually a function, so you are not required to use parentheses with it.
  9. After that we close the connection.

Conclusion :-

MySQL data can be retrieved using the SELECT statement. It is possible to retrieve data from specific columns in a table or from all columns at once.

I hope this article on PHP code to retrieve data from MySQL database and display helps you and the steps and method mentioned above are easy to follow and implement.