Thursday, 18 April 2013

How to connect PHP to MySQL ???


In web developement of php with mysql database it is must required to connect php form to the mysql database

$conn = mysqli_connect(“Server", “Username",“Password");



$conn is the varible in PHP.
mysqli_connect() :- is a method which is required to connect with the MySQL database
The attributes of the method are

Server – host name of server
Username - Username of Database
Password - Password of Database

Example :- 

if PC is works as a client and server then


$hostname='localhost';
$username='root';
$password='';
$conn = mysqli_connect($hostname,$username, $password) or die("Unable to connect to MySQL");
echo "<br>Now , Php for is connected to Mysql ";

              OR

$conn=mysqli_connect(@"localhost","root","“);


HTML and PHP code to Connect with MySQL using localhost and create new database using it.


<html>

<head>
<title>Demo</title>
</head>
<body>
Php form 
<?php
// Creating a connection to mysql

$hostname='localhost';
$username='root';
$password='';
$conn = mysqli_connect($hostname,$username, $password) 
  or die("Unable to connect to MySQL");
echo "<br>Now , Php for is connected to Mysql ";

$sql="CREATE DATABASE Database1";

if (mysqli_query($conn,$sql))
 {
 echo "<BR><BR>Database Database1 created successfully";
  }
else
  {
  echo "Error creating database: " . mysqli_error();
  }
?> 
</body>
</html>


No comments:

Post a Comment