PHP MySQL Select Data



Select data from MySQL database in PHP

In PHP web application we wish to select data from MySQL database then first establish connection between PHP web application and MySQL database. After establish a connection use SELECT statement to select data from MySQL database.

SELECT Statement

The SELECT statement is used to select data from a table  in the database.

SELECT Statement Syntax

The SELECT Syntax categorize in to two parts , first we retrieve all columns means full table data, second we retrieve some specific columns from a table.

Select all columns from table

SELECT * FROM table_name

Select some columns data from a table

SELECT col1, col2, col3 FROM table_name


PHP Example of Select data from database

<?php

$username = "user_name";
$password = "user_password";
$hostname = "localhost"; 
$database="School";

//connection to the mysql database,
$dbhandle = mysqli_connect($hostname, $username, $password,$database )
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//execute the SQL Statement
$result = mysqli_query($dbhandle, "SELECT ID, Name, City FROM StudentMst" );

//fetch tha data from the database 
while ($row = mysqli_fetch_array($result)) {
 echo "ID:" .$row{'ID'}." Name:".$row{'Name'}." City: ". $row{'City'}."<br>";
}
//close the connection
mysqli_close($dbhandle);

?>

The result  of above PHP MySQL example :

php-mysql example
PHP MySQL Connection Example using MYSQLi Extension

Leave a Reply

Your email address will not be published. Required fields are marked *