Oct 16, 2014

Connecting To MySQL Database Using PHP

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 '] ;
    }
?>

No comments:

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...