Posted by: Sourav | May 27, 2011

Database connection using php


<?php
//1.create a database connection
$connection=mysql_connect(“localhost”,”root”,”******”);

if(!$connection)
{
    die(“Database connection failed:”.mysql_error());
}
//2.selecting the database
$db_select=mysql_select_db(“souravphp_corp”,$connection);
if(!$db_select)
{
    die(“Database connection failed:”.mysql_error());
}

 

?>

 

 

<html>
    <head>
        <title>
            Creating database connection using php
        </title>
    </head>
    <body>
       <?php
       
       
       
        //3.perform database query
 
 $result=mysql_query(“select * from subjects”,$connection);      
       
       if(!$result)
{
    die(“Database connection failed:”.mysql_error());
}
 //4.Use returned data
while($row=mysql_fetch_array($result))
{
    echo $row[0].” “.$row[1].” “.$row[2].” “.$row[3].”<br />”;
}
       
        ?>
       
       
    </body>
</html>
<?php

//5.Close the connection
mysql_close($connection);
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: