Home Tutorial Php Phpdatabase Setting up Database with the help of PHP Scripts.

 
 

Share on Google+Share on Google+
Setting up Database with the help of PHP Scripts.
Posted on: January 23, 2010 at 12:00 AM
Advertisement
With the help of PHP Scripts, we can also create database, table as well as insert the information. As we discussed in the last tutorial that MySQL has its own standard set up to create database which is contained tables.

PHP Database Set Up Script

Part-5(c) : Setting up the database

2. Database Creation with the help of PHP Scripts.

With the help of PHP Scripts, we can also create database, table as well as insert the information. As we discussed in the last tutorial that MySQL has its own standard set up to create database which is contained tables.

Let's follow the following steps to create a database, tables and contact form with the help PHP scripts.

A. Database Creation

Firstly, we will create database called contact_roseindia and also set the hostname, username or password of my local server.

<?php

$username = "root";

$password = "";

$hostname = "localhost";

$database = "contact_roseindia";

$databasecreation="CREATE DATABASE ".$database;

$conn = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

mysql_query($databasecreation);

mysql_select_db($database,$conn);

?>

In the above code, I  replace the ?localhost? with the location of my server, ?root? with my username, ?? with my password.  You can also replace the setting of your computer. Then, I create the database called "contact_roseindia" and connect with the PHP.

B. Creation A Table

The next step before doing anything with the database is to create a table. A table is a place where we store all the related information. We can set up the different fields in a table, which we will be used later in the website. We can set up different tables in one database for a website.  

If you noticed in our previous tutorial creating a table in PHPMyAdmin is as simple as anything, just type the name, select the number of fields and click the button. But in this tutorial you will learn how to create your database, the whole creation and setup will be done in one command with the help of PHP Scripts.

Some people thinks that creating a table in PHP is really a difficult task. Yes, it is bit complicated but not too difficult, I will try to make it as simple as possible.

The following code should be used to create this table in PHP :-

<?php

$username = "root";

$password = "";

$hostname = "localhost";

$database = "contact_roseindia";

//connection to the database

$databasecreation="CREATE DATABASE ".$database;

$conn = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

mysql_query($databasecreation);

mysql_select_db($database,$conn);

$query="CREATE TABLE tbl_contacts(ID int(10) NOT NULL auto_increment, First_Name varchar(30) NOT NULL, Last_Name varchar(30) NOT NULL, Phone varchar(15) NOT NULL, Mobile varchar(15) NOT NULL, Fax varchar(15) NOT NULL, Email varchar(30) NOT NULL, Web varchar(50) NOT NULL, Address varchar(255) NOT NULL)";

mysql_query($query);

?>

In the above code we created a table called tbl_contacts and then stored into a PHP variable that is $query. At the end, I run the mysql_query($query).

 

C. Inserting Information

Till now, we created a database and table. Now, this is the time to insert the information or data in our database.

Let's see the following code below :

<?php

$username = "root";

$password = "";

$hostname = "localhost";

$database = "contact_roseindia";

//connection to the database

$databasecreation="CREATE DATABASE ".$database;

$conn = mysql_connect($hostname, $username, $password)or die("Unable to connect to MySQL");

mysql_query($databasecreation);

mysql_select_db($database,$conn);

$query="CREATE TABLE contacts(ID int(10) NOT NULL, First_Name varchar(30) NOT NULL, Last_Name varchar(30) NOT NULL, Phone varchar(15) NOT NULL, Mobile varchar(15) NOT NULL, Fax varchar(15) NOT NULL, Email varchar(30) NOT NULL, Web varchar(50) NOT NULL, Address varchar(255) NOT NULL)";

mysql_query($query);

$insert = "INSERT INTO contacts VALUES ('','Satya','Prakash','011-25156545','9810232323','011-25844242','satyapra02@gmail.com','www.sp02.com','Sector-3, Rohini New Delhi.')";

mysql_query($insert);

$result = mysql_query("select * from contacts");

echo "<table border='1'>

<tr>

<th>ID</th>

<th>First Name</th>

<th>Last Name</th>

<th>Phone</th>

<th>Mobile</th>

<th>Fax</th>

<th>Email</th>

<th>Web</th>

<th>Address</th>

</tr>";

while($row = mysql_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['ID'] . "</td>";

echo "<td>" . $row['First_Name'] . "</td>";

echo "<td>" . $row['Last_Name'] . "</td>";

echo "<td>" . $row['Phone'] . "</td>";

echo "<td>" . $row['Mobile'] . "</td>";

echo "<td>" . $row['Fax'] . "</td>";

echo "<td>" . $row['Email'] . "</td>";

echo "<td>" . $row['Web'] . "</td>";

echo "<td>" . $row['Address'] . "</td>";

echo "</tr>";

}

echo "</table>";

?>

 In the above code, we insert the information in the data directly through PHP Scripts. First of all, we used variable called $insert. to  assign the information.

INSERT INTO contacts VALUES

This code tells the PHP to insert into the table called contacts the values in the brackets which follow the entire information.

Advertisement

Related Tags for Setting up Database with the help of PHP Scripts.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 23, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com