Sunday 24 April 2016

Create a database and Connection with Database In PHP


How to create a database in PHP?



In PHP Create a Database steps.
1). Open a Wamp or Xamp Server.
2). Open any browser and write a "localhost" in URL.
3). After Open the localhost check on Page and click on phpmyadmin.
4). Write a new database name in "Create new database" and click on Create button.
5). Create a Table write a "Table name" and "Number Of Field ".
6). Click on "Go" button.
7). write a set Field name, Type, Length/Value, Index, Auto increment.
8).Click on " Save " button.

you can also create a table using another way through coading using Create Command and code execute using mysql_query(); method.

Crate a table using caoding

<?PHP
$cn=mysql_connect("localhost","root","");
$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";
mysql_query($cn,$tb);
$cn.close();
?>

using this coading you can create a table in database in PHP.

Click on link and see the video See In this video how to create a databse in PHP?


How to Create a Databse in PHP?  

How to Making a Connection with Database in PHP?

In the PHP Connect() method is used to create a connetion. Connect() method use like that mysql_connect( argument ); here, there are three argument Server name, Username, Password.
We can also use a mysqli_connect( argument ); here, also argument is same.

Syntax

mysql_connect(Server_name, User_name, Password);

Example

<?PHP
$con=mysql_connect("localhost","root","");
if (mysql_connect_errno($con))
  {
  echo "Failed to connect with MySQL: " . mysql_connect_error();
  }

mysql_close($con);
?> 

in this example first make a connection with database and then check the connetion is establish or not using mysql_connect_errno( $con ) in this line the $con is a Connetion Object. here check the if condition and any error is occured then print a message and exit a script. 

No comments:

Post a Comment