Setting up Database with the help of PHP Scripts.


 

Setting up Database 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.

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;

0

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

mysql_query($databasecreation);

mysql_select_db($database,$conn);

1

$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);

?>

2

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

3

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

4

$username = "root";

$password = "";

$hostname = "localhost";

5

$database = "contact_roseindia";

//connection to the database

$databasecreation="CREATE DATABASE ".$database;

6

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

mysql_query($databasecreation);

mysql_select_db($database,$conn);

7

$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','[email protected]','www.sp02.com','Sector-3, Rohini New Delhi.')";

8

mysql_query($insert);

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

echo "<table border='1'>

9

<tr>

<th>ID</th>

<th>First Name</th>

0

<th>Last Name</th>

<th>Phone</th>

<th>Mobile</th>

1

<th>Fax</th>

<th>Email</th>

<th>Web</th>

2

<th>Address</th>

</tr>";

while($row = mysql_fetch_array($result))

3

{

echo "<tr>";

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

4

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

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

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

5

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

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

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

6

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

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

echo "</tr>";

7

}

echo "</table>";

?>

8

 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.

9

Ads