select query in php mysql with example



Select query in php mysql with example

In previous  PHP post we learn to establish connection between PHP web application and MySQL database system. In this PHP tutorial we will learn to select a data from myql database with php form example.

Here, we design simple PHP form with a textbox and a button control. Here we select a data from database and display on webpage while click the select button.In a textbox we input integer id value then the Id wise record will be displayed on result screen.


PHP form Example for Select data

<html>
<head>
<title>PHP MySQL connection example</title>
</head>
<body>

<form method="post">
Enter Rcord ID : <input type="text" name="id"><br/>
<input type="submit" value="SELECT" name="Submit1"> <br/>


</form>
<?php
if(isset($_POST['Submit1']))
{ 
$username = "username";
$password = "password";
$hostname = "localhost"; 
$database="School";

//connection to the mysql database,
$dbhandle = mysqli_connect($hostname, $username, $password,$database);

if(!empty($_POST["id"]))
{
$result = mysqli_query($dbhandle, "SELECT ID, Name, City FROM StudentMst where ID=".$_POST["id"] );
}
else
{ 
$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);
}
?>
</body>
</form>

The result of above php form example :

PHPExample
PHP MySQL Example for select data

Leave a Reply

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