0% found this document useful (0 votes)
18 views8 pages

PHP UNIT 5

This document provides an overview of creating databases and tables using PHP and MySQL. It explains the concept of a database, outlines the steps to create tables, and includes example PHP code for connecting to a MySQL database and executing SQL commands. Additionally, it discusses the use of XAMPP as a local server for web development and details the process of creating a database and tables using MySQLi and PDO.

Uploaded by

Siddharth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

PHP UNIT 5

This document provides an overview of creating databases and tables using PHP and MySQL. It explains the concept of a database, outlines the steps to create tables, and includes example PHP code for connecting to a MySQL database and executing SQL commands. Additionally, it discusses the use of XAMPP as a local server for web development and details the process of creating a database and tables using MySQLi and PDO.

Uploaded by

Siddharth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Web Programming (PHP)

Unit 5: MYSQL

PHP | MySQL ( Creating Database )


What is a database?
Database is a collection of inter-related data which helps in efficient
retrieval, insertion and deletion of data from database and organizes the
data in the form of tables, views, schemas, reports etc. For Example,
university database organizes the data about students, faculty, and admin
staff etc. which helps in efficient retrieval, insertion and deletion of data
from it.

Creating Database Tables


To create tables in the new database you need to do the same thing as
creating the database. First create the SQL query to create the tables then
execute the query using mysql_query() function.

Example
Try out following example to create a table −

<?php

$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
die('Could not connect: ' . mysql_error());
}

Web Programming (PHP) 1


echo 'Connected successfully';

$sql = 'CREATE TABLE employee( '.


'emp_id INT NOT NULL AUTO_INCREMENT, '.
'emp_name VARCHAR(20) NOT NULL, '.
'emp_address VARCHAR(20) NOT NULL, '.
'emp_salary INT NOT NULL, '.
'join_date timestamp(14) NOT NULL, '.
'primary key ( emp_id ))';
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
die('Could not create table: ' . mysql_error());
}

echo "Table employee created successfully\n";

mysql_close($conn);
?>

In case you need to create many tables then its better to create a text file
first and put all the SQL commands in that text file and then load that file into
$sql variable and excute those commands.

Consider the following content in sql_query.txt file

CREATE TABLE employee(


emp_id INT NOT NULL AUTO_INCREMENT,
emp_name VARCHAR(20) NOT NULL,
emp_address VARCHAR(20) NOT NULL,
emp_salary INT NOT NULL,
join_date timestamp(14) NOT NULL,
primary key ( emp_id ));

<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';

Web Programming (PHP) 2


$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
die('Could not connect: ' . mysql_error());
}

$query_file = 'sql_query.txt';

$fp = fopen($query_file, 'r');


$sql = fread($fp, filesize($query_file));
fclose($fp);

mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
die('Could not create table: ' . mysql_error());
}

echo "Table employee created successfully\n";


mysql_close($conn);
?>

PHP Database connection


The collection of related data is called a database. XAMPP stands for cross-
platform, Apache, MySQL, PHP, and Perl. It is among the simple light-weight
local servers for website development.
Requirements: XAMPP web server procedure:

Start XAMPP server by starting Apache and MySQL.

Write PHP script for connecting to XAMPP.

Run it in the local browser.

Database is successfully created which is based on the PHP code.

Web Programming (PHP) 3


In PHP, we can connect to the database using XAMPP web server by using
the following path.

"localhost/phpmyadmin"

Steps in Detail:

Open XAMPP and start running Apache, MySQL and FileZilla

Now open your PHP file and write your PHP code to create database
and a table in your database.

PHP code to create a database:

<?php

// Server name must be localhost


$servername = "localhost";

// In my case, user name will be root


$username = "root";

// Password is empty
$password = "";

Web Programming (PHP) 4


// Creating a connection
$conn = new mysqli($servername,
$username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failure: "
. $conn->connect_error);
}

// Creating a database named geekdata


$sql = "CREATE DATABASE geekdata";
if ($conn->query($sql) === TRUE) {
echo "Database with name geekdata";
} else {
echo "Error: " . $conn->error;
}

// Closing connection
$conn->close();
?>

Save the file as “data.php” in htdocs folder under XAMPP folder.

Then open your web browser and type localhost/data.php

Web Programming (PHP) 5


Finally the database is created and connected to PHP.
If you want to see your database, just type localhost/phpmyadmin in the
web browser and the database can be found.

PHP | MySQL ( Creating Table )


What is a table?

In relational databases, and flat file databases, a table is a set of data


elements using a model of vertical columns and horizontal rows, the cell
being the unit where a row and column intersect. A table has a specified
number of columns, but can have any number of rows.

Creating a MySQL Table Using MySQLi and PDO


We have already learned about creating databases in MySQL from PHP
in this article. The steps to create table are similar to creating databases.
The difference is instead of creating a new database we will connect to
existing database and create a table in that database. To connect to an

Web Programming (PHP) 6


existing database we can pass an extra variable “database name” while
connecting to MySQL.
The CREATE TABLE statement is used to create a table in MySQL.
In this article, a table named “employees”, with four columns: “id”,
“firstname”, “lastname” and “email” will be created.
The data types that will be used are :

1. VARCHAR:Holds a variable length string that can contain letters,


numbers, and special characters. The maximum size is specified in
parenthesis.

2. INT :he INTEGER data type accepts numeric values with an implied
scale of zero. It stores any integer value between -2147483648 to
2147483647.

The attributes that are used along with data types in this article are:

1. NOT NULL: Each row must contain a value for that column, null values
are not allowed.

2. PRIMARY KEY: Used to uniquely identify the rows in a table. The column
with PRIMARY KEY setting is often an ID number.

Creating tables in three different versions are described below:

Creating table using MySQLi Object-oriented ProcedureSyntax :

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "newDB";

// checking connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql code to create table

Web Programming (PHP) 7


$sql = "CREATE TABLE employees(
id INT(2) PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)";

if ($conn->query($sql) === TRUE) {


echo "Table employees created successfully";
} else {
echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

Web Programming (PHP) 8

You might also like