PHP development Code

Unknown | 3:12 PM

//-----------------DATABASE CONNECTIVITY IN PHP -----------------//


//---------- file name connection.php-----
 <?php
$hostname = "localhost";
$username = "root";
$dbname = "database_name";
$password = "password";
           
// ------Function for database connection---------           
function connection()
{
    $conn=mysql_connect($hostname , $username , $password );
    mysql_select_db( $dbname , $conn );
    return $conn;
}

// ------Function to fetch data from database ---------   
function show($sql)
{
    $conn=conn();      // call function to connect to database
    $arr=array();

    $result=mysql_query($sql) or die("Error: table not found");
    if($result)
    {
        while($row=mysql_fetch_array($result))
            $arr[]=$row;
    }
   
     mysql_close($conn);
    return $arr;
}





?>

You can call the above function through the below code

//--------------result.php-----------
<?php

include('connection.php');

// fetch all the records from the specified  table in ascending order of id.

    $sql="SELECT * FROM  `tbl_name` ORDER BY  `tbl_name`.`id` ASC";  
    $rs=show($sql);

foreach($rs as $v)
{
 echo"<span> " '.$v.' "</span><br />";        // to print all the records within html tags.
}

?>


if you have any query regarding this code then you can either e-mail me or contact me

+91 8750749655
avikanak.bkr@gmail.com

Thank you.


No comments:

Post a Comment

Avinash Kumar Singh © 2013. All Rights Reserved | Powered by-Blogger