Unit-5 Working With Database in PHP
Unit-5 Working With Database in PHP
Database:
A database management system(DBMS) is system software for creating and
managing database.
The DBMS provides users and programmers with a systematic way to create,
retrieve, update and manage data.
Now-a-days, we use relational database management systems (RDBMS) to store
and manage huge volume of data. This is called relational database because all
the data is stored into different tables and relations are established using
primary keys or foreign keys.
A Relational Database Management System(RDBMS) is a software that:
Enables you to implement a database with tables, columns and indexes.
Guarantees the Referential Integrity between rows of various tables.
Updates the indexes automatically.
Interprets an SQL query and combines information from various tables.
RDBMS Terminology
Database: A database is a collection of tables, with related data.
Table: A table is a collection of data elements organized in terms of rows and
columns.
Column: In relational table, a column is a set of value of a particular type.
Row: A row (tuple or record) is a group of related data.
Redundancy: Redundancy means having multiple copies of same data in the
database.
PrimaryKey: A primary key is used to uniquely identify the table record and
cannot contain duplicate data and null value.
ForeignKey: A foreign key is used to derive value from primary key of other
table.
Composite key: A composite key is a combination of two or more columns in a
table that can be used to uniquely identify each row in the table.
Index: A database index is a data structure that improves the speed of operations
in a table.
What is phpMyAdmin?
Php MyAdmin is one of the most popular applications for MySQL databases
management.
It is a free tool written in PHP.
Through this software you can create, alter, drop, delete, import and export
MySQL database tables.
You can run MySQL queries, optimize, repair and check tables, change collation
and execute other database management commands.
Step3: Create Database: Type your database name, then click ‘create’ Button.
Step4: Database ending: Your database is created. You can find that one on left
side.
1) Numeric:
Type Use For Size
A very small integer The signed range is - 128 to 127.
TINY INT
The unsigned range is - 0 to 255.
A small integer The signed range is - 32768 to 32767.
SMALL INT
The unsigned range is O to 65535.
A medium sized The signed range is -8388608 to
MEDIUM
integer 8388607.
INT
The unsigned range is O to 16777215.
A normal sized The signed range is – 2147483648
integer to 2147483647.
INT
The unsigned range is 0 to
4294967295.
A large integer The signed range is -
9223372036854775808 to
BIGIN 9223372036854775807.
The unsigned range is 0 to
18446744073709551615.
Parameter Description
Server(host) Specifies a server name.
username Specifies the MySQL username.
password Specifies the MySQL password
Database name The name of MySQL database to use.
Example:
<?php
/*$host="localhost";
$username="root";
$password="";
$database="db_mydatabase";
Prepared By: Prof. Amit Rathod Page No.8
$conn=mysqli_connect($host,$username,$password,$database);*/
$conn=mysqli_connect("localhost","root"," ", "db_mydatabase ");
if(!$conn)
{
die("Connection Failed:".mysqli_connect_error());
}
else
{
echo "Connected Successfully.....";
mysqli_close($conn);
}
?>
2) mysqli_select_db()
The mysqli_select_db() function is used to change the default database for
the connection.
Returns TRUE on success and FALSE on failure.
Syntax:
mysqli_select_db(connection,databasename)
Parameter Description
connection Required. Connection object name
databasenam Required. Specifies the default database tobe used
Example e
<?php
$conn=mysqli_connect("localhost","root","");
$databse=mysqli_select_db($conn,"db_mydatabase");
if(!$databse)
{
die("Connection Failed:".mysqli_connect_error());
}
else
{
echo "Connected Successfully.....";
mysqli_close($conn);
}
?>
3) mysqli_query()
The mysqli_query() function executes a query on a MySQLdatabase.
Syntax:
mysqli_query(connection,query)
}
$db=mysqli_select_db($conn,"student");
$query="select * from studentinfo";
$result=mysql_query($conn, $query);
if(!$result)
{
echo"Database query failed";
}
mysqli_close($conn);
?>
4) mysqli_num_rows()
The mysqli_num_rows() function returns the number of rows in a resultset.
Syntax:
mysqli_num_rows(result)
Parameter Description
result Required. Spec ifies a resultset identifier returned by
mysqli_query()
Example
<?php
$conn=mysqli_connect("localhost","root",'"');
if(!$conn)
{
die("Connection Failed:".mysqli_connect_error());
}
else
Prepared By: Prof. Amit Rathod Page No.10
{
echo "Connected Successfully.....";
$db=mysqli_select_db($conn,"student");
$query="select * from student info";
$result=mysql_query($conn,$query);
$rowcount=mysqli_num_rows($result);
echo"Total Row:".$rowcount;
mysqli_close($conn);
?>
5) mysqli_fetch_array()
The mysql_fetch_array () function fetches a result row as an associative
array, a numeric array, or both.
Returns an array of strings that corresponds to the fetched row.
NULL if there are no more rows in result-set.
Syntax:
mysqli_fetch_array(result)
Parameter Description
result Required. Specifies a resultset identifier returned by
mysqli_query()
Example
<?php
$conn=mysqli_connect("locaIhost","root","");
if(!$conn)
{
die("Connection Failed:".mysqli_connect_error());
}
else
{
echo "Connected Successfully.....";
}
$db=mysql_select_db($conn,"student");
$query="select StudentlD, Name, Email, Gender from studentinfo";
$result=mysqli_query($conn,$qry);
while($row=mysqli_fetch_array($result))
{
echo $row[O]."<br/>";
Prepared By: Prof. Amit Rathod Page No.11
echo $row['Name']."<br/>";
}
?>
Note: Field names returned from this function are case-sensitive.
6) mysqli_fetch_assoc()
The mysqli_fetch_assoc() function fetches a result row as an associative
array.
Returns anassociative array of strings representing the fetched row.
NULL if there are no more rows in result-set.
Syntax:
mysqli_fetch_assoc(result)
Parameter Description
result Required. Specifies a resultset identifier returned by
mysqli_query()
Example
<?php
$conn=mysqli_connect("locaIhost","root","");
if(!$conn)
{
die("Connection Failed:".mysqli_connect_error());
}
else
{
echo "Connected Successfully.....";
}
$db=mysqli_select_db($conn,"student");
$query="select StudentlD, Name, Email, Gender from studentinfo";
$result=mysqli_query($conn,$query);
while($row=mysqli_fetch_assoc($result)
{
echo $row['StudentlD']."<br/>”;
echo $row['Name']."<br/>";
}
?>
Note: Field names returned from this function are case-sensitive.
7) mysqli_close()
The mysqli_close() function closes a previously opened database
connection.
Syntax:
mysqli_close(connection)
Parameter Description
connection Optional. Before performing any operation on a
MySQL database, it is required to set a connection to
the mysql database you want to work with. And this is
done by mysqli_connect()function.
Example
<?php
$conn=mysql_connect("localhost", "root","");
$db=mysql_select_db( "student");
if(!$db)
{
echo "Something else wrong".mysql_error($conn);
die;
}
?>