Connect PHP with Database(MySQL)

 We can connect databases for example MySQL and manipulate with PHP. 

Following are the steps to connect database with PHP.

  • Create MySQL database at the localhost.
  • Create a folder in htdocs.
  • Create Database Connection File in PHP.
  • Create new php file to check database connection.
  • Run the file.
Lets's create a new PHP file and sabe it with the name db_connection.php.

Now, write the following code in db_connection.php file

<?php
function OpenCon()
{
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$db="example";
$conn=new mysqli($dbhost,$dbpass,$db) or die("Connection Failed:%s\n".conn->error);
return $conn;
}
function CloseCon($conn)
{
$conn->close();
}

This code will connect to database. 
?>


The best database program contains the following queries :
  • Create database
  • Create table
  • Alter table
  • Add records in a table
  • Search records in a table
  • Delete a record.
  • Update a record
php,mysql




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.