How To Use SQL SELECT STATEMENT With Example Using PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

SQL SELECT Statement used to retrieves data from data base and data is returned in a table like structure know as result-set.SQL SELECT Statement used commonly in every SQL queries.

SQL SELECT Statement

Syntax for SQL SELECT Statement.

SELECT column1, column2, ...
FROM table_name;


above syntax SELECT only column names ( column1, column2,) from the table if we want to SELECT entire table data so for that we need to use below Syntax.
 

SELECT * FROM table_name;



Let's Learn.
1.Create database and  table.
Student table.

StudentID StudentName Address Country CourseID DOB
1 Liam 23 Andrew road USA 5 1995-08-21
2 Andrew 2415 Anthony  market str Germany 6 NULL
4 Ryan 18 Christopher sq. INDIA 2 NULL
5 Charles 878 Eli Berguvsvägen 8 UK 1 1995-01-11

for this above student table we need to use SQL SELECT Statement.


Example(1)

SELECT StudentName,Address,DOB
FROM student

above SQL statement selects the "StudentName" ," Address"and "DOB" columns from the "student " table.


Example(2)

SELECT * FROM student

above SQL statement selects the all columns from the "student " table.


2.Complete code of SQL SELECT Statement  in PHP script.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to use SQL SELECT Statement.</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>SQL SELECT Statement</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "management");

        if ($connect->connect_error) {
            die("Connection failed: " . $connect->connect_error);
        }

        $sql_1 = "SELECT StudentName,Address,DOB  FROM student";
        $sql_2 = "SELECT * FROM student";


        echo "<h4 class='text-danger'>Example(1)</h4>";
        if($result_1 = mysqli_query($connect, $sql_1)){
            if(mysqli_num_rows($result_1) > 0){
                echo "<table class='table'>
                    <tr>
                     <th>StudentName</th>
                     <th>Address</th>
                     <th>DOB</th>
                    </tr>
                    ";
                while($row = $result_1->fetch_assoc()){
                    echo "<tr>
                   <td>".$row['StudentName'] /*student table*/ ."</td> 
                   <td>".$row['Address']  ."</td>
                   <td>".$row['DOB']  ."</td>
                  </tr>";
                }
                echo "</table>";
            } else{
                echo "No records found.";
            }
        }
        echo "<h4 class='text-danger'>Example(2) </h4>";
        if($result_1 = mysqli_query($connect, $sql_2)){
            if(mysqli_num_rows($result_1) > 0){
                echo "<table class='table'>
                    <tr>
                     <th>StudentID</th>
                     <th>StudentName</th>
                     <th>Address</th>
                     <th>Country</th>
                     <th>CourseID</th>
                     <th>DOB</th>
                    </tr>
                    ";
                while($row = $result_1->fetch_assoc()){
                    echo "<tr>
                    <td>".$row['StudentID'] /*student table*/ ."</td> 
                    <td>".$row['StudentName'] /*student table*/ ."</td> 
                    <td>".$row['Address'] /*student table*/ ."</td> 
                    <td>".$row['Country'] /*student table*/ ."</td> 
                   <td>".$row['CourseID']  ."</td>
                   <td>".$row['DOB']  ."</td>
                  </tr>";
                }
                echo "</table>";
            } else{
                echo "No records found.";
            }
        }

        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post