Posted by: Sourav | June 14, 2011

PHP database connection practise


<?php
//create a ddatabse connection
$connection=mysql_connect(“localhost”,”root”,”123456″);
if(!$connection)
{
    die(“database connection failed”.mysql_error());
}
//selecting which database to use

$db_select=mysql_select_db(“sourav_test”,$connection);
if(!$db_select)
{
    die(“database connection failed”.mysql_error());
}

?>
<html>
    <head>
        <title>
            Database connection practise
        </title>
    </head>
    <body>
        <?php
       //performing database query
      
       $query=mysql_query(“select * from subjects”,$connection);
       if(!$query)
{
    die(“database connection failed”.mysql_error());
}

//returning data from database

while($result=mysql_fetch_array($query))
{
    echo $result[1].” “.$result[2].”<br />”;
}
    ?>   
    </body>
</html>

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: