0% found this document useful (0 votes)
3 views9 pages

What Is A Database

A database is an application that stores data in a structured format, commonly using relational database management systems (RDBMS) which organize data into tables with relationships established through keys. MySQL is a popular open-source RDBMS that allows users to create, manage, and manipulate databases using SQL. The document also provides examples of PHP functions for connecting to MySQL, creating databases and tables, and performing CRUD operations.

Uploaded by

mind59024
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)
3 views9 pages

What Is A Database

A database is an application that stores data in a structured format, commonly using relational database management systems (RDBMS) which organize data into tables with relationships established through keys. MySQL is a popular open-source RDBMS that allows users to create, manage, and manipulate databases using SQL. The document also provides examples of PHP functions for connecting to MySQL, creating databases and tables, and performing CRUD operations.

Uploaded by

mind59024
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/ 9

What is a Database?

A database is a separate application that stores a collection of data. Each


database has one or more distinct APIs for creating, accessing, managing,
searching and replicating the data it holds.

A database is collection of huge amount of data. Nowadays, 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 other keys
known as 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 matrix with data. A table in a database looks like a


simple spreadsheet.

 Column − One column (data element) contains data of one and the same
kind, for example the column postcode.

 Row − A row (= tuple, entry or record) is a group of related data, for


example the data of one subscription.

 Primary Key − A primary key is unique. A key value can not occur
twice in one table. With a key, you can only find one row.

 Foreign Key − A foreign key is the linking pin between two tables.
 Compound Key − A compound key (composite key) is a key that
consists of multiple columns, because one column is not sufficiently
unique.

MySQL Database

MySQL is a fast, easy-to-use RDBMS being used for many small and big
businesses. MySQL is developed, marketed and supported by MySQL AB,
which is a Swedish company. MySQL is becoming so popular because of many
good reasons −

 MySQL is released under an open-source license. So you have nothing to


pay to use it.

 MySQL is a very powerful program in its own right. It handles a large


subset of the functionality of the most expensive and powerful database
packages.

 MySQL uses a standard form of the well-known SQL data language.

PHP mysqli_connect()

PHP mysqli_connect() function is used to connect with MySQL database. It


returns resource if connection is established or null.

Syntax

resource mysqli_connect (server, username, password)

PHP mysqli_close()

PHP mysqli_close() function is used to disconnect with MySQL database. It


returns true if connection is closed or false.

Syntax

bool mysqli_close(resource $resource_link)

--------------------------------------------------------------

<?php

$host ="localhost";
$user ="";

$pass ="";

$conn = mysqli_connect($host, $user, $pass);

if(! $conn)

die("Could not connect: " . mysqli_error());

echo "Connected successfully";

mysqli_close($conn);

?>

------------------------------------------------------------------------

PHP MySQL Create Database

<?php

$sn="localhost";

$us="root";

$pa="";

$database="bit2";

$connection=new mysqli($sn,$us,$pa);

if($connection->connect_errno !=0)

die("Connection Failed:");

$sql="create database bit2";

if($connection->query($sql))
{

echo "databaase created sucessfully";

else

echo "Error".$connection->error;

$connection->select_db($database);

$connection->close();

?>

PHP MySQL Create Table

<?php

$sn="localhost";

$us="root";

$pa="";

$database="bit2";

$connection=new mysqli($sn,$us,$pa,$database);

if($connection->connect_errno !=0)

die("Connection Failed:");

$sql="create table student (roll int ,sname char(20),address char(20))";

if($connection->query($sql))

{
echo "Table created sucessfully";

else

echo "Error".$connection->error;

?>

----------------------------------------------------------------------------------

Insert record in Table

<?php

$sn="localhost";

$us="root";

$pa="";

$database="bit2";

$connection=new mysqli($sn,$us,$pa,$database);

if($connection->connect_errno !=0)

die("Connection Failed:");

echo"Database connection successfuly";

$sql="INSERT INTO student values(1,'Ram','Birgunj')";

if($connection->query($sql))

echo "Record inserted sucessfully";


}

else

echo "Error".$connection->error;

?>

-----------------------------------------------------------------------------------------

Select specific record from table

<?php

$sn="localhost";

$us="root";

$pa="";

$database="bit2";

$connection=new mysqli($sn,$us,$pa,$database);

if($connection->connect_errno !=0)

die("Connection Failed:");

echo"Database connection successfuly";

$sql="select * from student where roll=1";

$result=$connection->query($sql);

if($result->num_rows>0)

$row=$result->fetch_assoc();
print_r($row);

?>

Update record in Table

<?php

$sn="localhost";

$us="root";

$pa="";

$database="bit2";

$connection=new mysqli($sn,$us,$pa,$database);

if($connection->connect_errno !=0)

die("Connection Failed:");

echo"Database connection successfuly";

$sql="UPDATE student SET sname='Raj' Where roll=1";

$result=$connection->query($sql);

echo " Record update successfully";

?>

Delete record from Table

<?php

$sn="localhost";

$us="root";

$pa="";
$database="bit2";

$connection=new mysqli($sn,$us,$pa,$database);

if($connection->connect_errno !=0)

die("Connection Failed:");

echo"Database connection successfuly";

$sql="Delete from student Where roll=1";

$result=$connection->query($sql);

echo " Record deleted successfully";

?>

php.ini File Configuration


At the time of PHP installation, php.ini is a special file provided as a default
configuration file. It’s very essential configuration file which controls, what a
user can or cannot do with the website. Each time PHP is initialized,
the php.ini file is read by the system. Sometimes you need to change the
behavior of PHP at runtime, then this configuration file is to use.

All the settings related to register global variables, upload maximum size,
display log errors, resource limits, the maximum time to execute a PHP script
and others are written in a file as a set of directives which helps in declaring
changes.

It helps in easy administration of web server using these configuration files. We


can also write our own custom configuration files.

Using PHP To Backup My SQL Database


It is always good practice to take a regular backup of your database. There are
three ways you can use to take backup of your MySQL database.

 Using SQL Command through PHP.


 Using MySQL binary mysqldump through PHP.

 Using phpMyAdmin user interface.

We can execute SQL SELECT command to take a backup of any table. To take
a complete database dump we will need to write separate query for separate
table. Each table will be stored into separate text file.

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = ‘';

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

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

$table_name = "employee";
$backup_file = "/tmp/employee.sql";
$sql = "SELECT * INTO OUTFILE '$backup_file' FROM
$table_name";

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

if(! $retval ) {
die('Could not take data backup: ' . mysql_error());
}

echo "Backedup data successfully\n";

mysql_close($conn);
?>

You might also like