MySQL Select Data

  • Select From query is used to fetch or retrieve data from table.

Syntax

Syntax1
  • To fetch all the entries from table:
  • SELECT * FROM tableName;
Syntax2

To retrieve all the entries of the columns that have been specified in the query:

SELECT columnNames FROM tableName;

Example

We have following table with us-

idnamesalarycontact
1John250007777777
2Peter400009999999
  • Now we have to retrive data from the above table using following code:
<!DOCTYPE html>

<html>

<body>

<?php

$connect=mysqli_connect("localhost","root","","firstdb");

$query="SELECT * FROM employee";

$result=mysqli_query($connect,"SELECT * FROM employee");

while($rows=mysql_fetch_array($result)){

echo "<br>Name: ".$rows['name'];

echo "<br>Salary: ".$rows['salary']."<br>";}

?>

</body>

</html>

Output

Name: John
Salary: 25000

Name: Peter
Salary: 40000
  • In the program, a SELECT query is executed which should retrieve all the data from the table employee. The data is saved in the variable '$result'. Now we will fetch the rows one by one from this variable and store it in a variable '$rows'. Every time the while loop loops, $rows gets the next entries of next row. This variable '$rows' is an array which contains the entries of the current row. These entries can be accessed using the column names as index of array. For example $rows['name'], $rows['id']