Friday, 12 August 2016

Connect to MySql database using PHP

First before entering to this session, lets see what needed to be installed


  • Localhost Server - ( XAMPP or WAMPP or MAMP or LAMPP {According to your OS platform} )
  • Code Editor - whatever you like

After installing this, go to localhost/phpmyadmin in web browser 





Go to database section and create a database, for example lets create a database name Sample

lets enter into code

create a folder connection inside htdocs 

create a file database.php inside the folder connection

<?php

//lets declare a variable $connect to connect to the server

$connect = mysql_connect("localhost","root","password");  
// localhost refers to host address
// root refers to the database username
// password refers to database password

$db = mysql_select_db("Sample",$connect);
// $connect directs to the server connection
// Here Sample refers to our database name

// to check our server connection

if($connect)
{
    echo "Server connected successfully";
}
else
{
    echo "Problem in server connection";
}

// to check our database connection

if($db)
{
    echo "Database connected successfully";
}
else
{
    echo "Error in Database connection";
}

?>
  



Just run this code in browser localhost/connection/database.php

You can remove this if conditions, if it works successfully.



It is cool right ?


No comments:

Post a Comment