Viewing the table using SELECT command

The another way of seeing the table inside a database is to select the table from the database using the SELECT command, which shows the table. We can only fetch the data in the database and can show the data. That is it does not display it as a table. The creation of these data to view like a common table in MySQL is based on the skills of the developer.

In the below code, the data is displayed in the form of a table.

<?php
$servername="localhost";
$username="root";
$password="";
$dbname="mydb";
//connecting...
$conn=new mysqli($servername,$username,$password,$dbname);
//checking...
if($conn->connect_error)
{
    die("connection failed:".$conn->connect_error);
}
//inserting into ting table
$sql="SELECT * FROM firsttable";
$result=$conn->query($sql);

if($result->num_rows>0)
{
    echo "<table border=1>";
    echo "<tr><th>number</th><th>name</th></tr>";
    while($row=$result->fetch_assoc())
    {
     echo "<tr><td>".$row["num"]."</td><td>".$row["user"]."</td></tr>";
    }
    echo "</table>" ;
}
else
{
    echo "0 rows available";
}

$conn->close();
?>

Output:




The table tag and the sub tags of the table tags are used using the echo statement which can be used to add any types of tags inside the php.The $row["column_name"] is used to fetch the data of corresponding column with respect to the current column.The fetch_assoc() function fetches a result row as an associative array.

Prev
Next


Comments

Popular posts from this blog

Node.js Cheat Sheet

Codeigniter ! Simple But Powerful

Bootstrap ? What is it ?