All TalkersCode Topics

Follow TalkersCode On Social Media - 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 :-

$servername = "localhost";
$username = "root";
$password = "";
$databasename = "TalkersCode";
$conn = new mysqli ($servername,
      $username, $password, $databasename);
if ($conn->connect_error) {
      die ("Connection failed: ". $conn->connect_error);
$query = "SELECT * FROM Student Details;";
$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.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪