This article will demonstrate to you, how to connect to MySQL database using Php.First of all to connect to MySQL database you can simply use following script.
$link = mysql_connect("HOST_NAME", "USERNAME", "PASSWORD") or die(mysql_error());
$db = mysql_select_db("railway") or die(mysql_error());
After connecting to the database you can execute your MySQL queries as you want.
To read data from database you can use following codes.
$query = "SELECT * FROM `Employee` ";
$ResultData = mysql_query($query) or die('error');
while($RowCity = mysql_fetch_array($ResultDistrict))
{
$FirstName = $RowCity['FirstName '] ;
$LastName = $RowCity['LastName '] ;
}
As a good practice always keep your mysql_connect and mysql_connect_db queries in a separate Php file and include it in your page.To include php files in your current php file you can use include 'MYFILE.php'; code.
Following example will demonstrate you to use a separate php file to keep connection script.
Connect.php
<?php
$link = mysql_connect("HOST_NAME", "USERNAME", "PASSWORD") or die(mysql_error());
$db = mysql_select_db("railway") or die(mysql_error());
?>
Example.php
<?php
//MySQL Database Connect
include 'Connect.php';
//Getting Data From Another Form
$EmpId= $_POST["username"];
//Reading The Data
$query = "SELECT * FROM `Employee` WHERE `EmpId`="$EmpId;
$ResultData = mysql_query($query) or die('error');
while($RowCity = mysql_fetch_array($ResultDistrict))
{
$FirstName = $RowCity['FirstName '] ;
$LastName = $RowCity['LastName '] ;
}
?>
Subscribe to:
Post Comments (Atom)
JWT Token Decode Using Jquery
When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...
-
In this blog post Ill explain you the logic behind the Sri Lankan National Identity Card (NIC). Sri Lanka now has Old NIC No Format as well ...
-
In This Article I Will Demonstrate You To Create A Simple Chat Program Using Java Remote Method Invocation(RMI). Following Program Supports ...
-
In a windows form application sometimes you may want to import a excel file to a data grid view. In this blog post Ill demonstrate to you gu...
No comments:
Post a Comment