Unit - V-Wt-Php-Iib - Com (Ca)
Unit - V-Wt-Php-Iib - Com (Ca)
WEB TECHNOLOGY(PHP)
UNIT- V
Working with Database and SQL : Introducing Database and SQL- Using MySQL-Adding
and modifying Data-Handling Errors – Using SQLite Extension and PDO Extension.
Introduction XML - Simple XML and DOM Extension.
Introduction to Database
A database is a collection of interrelated data that helps in the efficient retrieval, insertion,
and deletion of data from the database and organizes the data in the form of tables, views,
schemas, reports, etc. For Example, a university database organizes the data about
students, faculty, admin staff, etc. which helps in the efficient retrieval, insertion, and
deletion of data from it.
A Database Management System (DBMS) is a software system that is designed to manage
and organize data in a structured manner. It allows users to create, modify, and query a
database, as well as manage the security and access controls for that database. DBMS
provides an environment to store and retrieve data in convenient and efficient manner.
History of SQL
SQL was invented in the 1970s based on the relational data model. It was initially known as
the structured English query language (SEQUEL). The term was later shortened to SQL.
Oracle, formerly known as Relational Software, became the first vendor to offer a
commercial SQL relational database management system.
Structured query language (SQL) is a programming language for storing and processing
information in a relational database. A relational database stores information in tabular form,
with rows and columns representing different data attributes and the various relationships
between the data values. You can use SQL statements to store, update, remove, search, and
retrieve information from the database. You can also use SQL to maintain and optimize
database performance.
Importance of SQL
Structured query language (SQL) is a popular query language that is frequently used in all
types of applications. Data analysts and developers learn and use SQL because it integrates
well with different programming languages. For example, they can embed SQL queries with
the Java programming language to build high-performing data processing applications with
major SQL database systems such as Oracle or MS SQL Server. SQL is also fairly easy to
learn as it uses common English keywords in its statements
o Structure query language is not case sensitive. Generally, keywords of SQL are
written in uppercase.
o Statements of SQL are dependent on text lines. We can use a single SQL statement on
one or multiple text line.
SQL process:
o When an SQL command is executing for any RDBMS, then the system figure out the
best way to carry out the request and the SQL engine determines that how to interpret
the task.
o In the process, various components are included. These components can be
optimization Engine, Query engine, Query dispatcher, classic, etc.
o All the non-SQL queries are handled by the classic query engine, but SQL query
engine won't handle logical files.
The DDL Languages or commands are categorized into five commands which are widely
used in the SQL queries:
CREATE Command :This DDL command allows us to create the new table, function,
stored procedure, and other database objects.
The following SQL query creates the new Mobile_Details table using CREATE DDL
command:
ALTER Command : This DDL command allows us to modify the structure of database
objects.
The following SQL query adds the new column in the Mobile_Details table using ALTER
DDL command:
DROP Command : This DDL command allows us to remove the table definition and
data from the SQL systems.
The following SQL query deletes the Mobile_Details table using the DROP DDL command:
TRUNCATE Command : This DDL command allows the database users to remove all
the existing records from the table.
The following SQL query deletes all the inserted records from the Mobile_Details table using
the TRUNCATE DDL command:
RENAME Command : This DDL command allows the users to change the name of the
existing table.
The following SQL query changes the name of Mobile_Details table to Mobile_Records table
using the RENAME DDL command:
Data Manipulation languages allow database users to change the existing data of the tables.
We can use this type of language when we want to access the record, insert the new record,
update the record, and delete the existing values from the tables.
Following are the four DML Languages or commands used in the SQL queries:
SELECT Command :This DML command allows us to access the stored records from the
tables. We can also use the condition in the SELECT command for accessing the
particular rows.
The following SQL query shows the records of the Mobile_Records table using the SELECT
DML command:
INSERT Command :This DML command allows the database users to insert the new
record or rows in the tables.
The following SQL query inserts the single record of mobile into the Mobile_Records table
using the INSERT DML command:
The following SQL query updates the values of the Mobile_Records table using the
UPDATE DML command:
DELETE Command : This DML command allows the database users to delete a
particular record or row from the tables.
The following SQL query deletes the values from the Mobile_Records table using the
DELETE DML command:
MySQL
Database Queries
The query above selects all the data in the "LastName" column from the "Employees" table.
Create a MySQL
Each table should have a primary key column (in this case: the "id" column). Its value must
be unique for each record in the table.
The following examples shows how to create the table in PHP:
CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
)";
Insert Data
The INSERT INTO statement is used to add new records to a MySQL table:
Syntax
To alter/modify the table use the ALTER TABLE syntax:
ALTER TABLE table_name
clause [column_name] [datatype];
Here, the clause is the operational clause of the ALTER TABLE statement. Some key
clauses of the ALTER TABLE statement are:
Below are the examples of ALTER TABLE statement. These examples demonstrates
different use cases and shows how to use ALTER TABLE statement in SQL.
The following SQL query adds an “Email” column to the “Students” table:
ALTER TABLE Students
ADD Email varchar(255);
The following query deletes the “Email” column from “Students” table:
ALTER TABLE Students
DROP COLUMN Email;
ROLL_NO NAME
1 Ram
2 Abhi
3 Rahul
4 Tanu
1 Ram
2 Abhi
3 Rahul
4 Tanu
Query:
ALTER TABLE Student
DROP COLUMN COURSE;
Output:
1 Ram
2 Abhi
3 Rahul
4 Tanu
DECLARE
declarations section;
BEGIN
executable command(s);
EXCEPTION
WHEN exception1 THEN
statement1;
WHEN exception2 THEN
statement2;
[WHEN others THEN]
/* default exception handling code */
END;
RAISE_APPLICATION_ERROR :
It is used to display user-defined error messages with error number whose range is in
between -20000 and -20999. When RAISE_APPLICATION_ERROR executes it returns
error message and error code which looks same as Oracle built-in error.
Named system exceptions: They have a predefined name by the system like
ACCESS_INTO_NULL, DUP_VAL_ON_INDEX, LOGIN_DENIED etc. the list is quite
big.
1.NO_DATA_FOUND: It is raised WHEN a SELECT INTO statement returns no rows.
2.TOO_MANY_ROWS:It is raised WHEN a SELECT INTO statement returns more than
one row.
3.VALUE_ERROR:This error is raised WHEN a statement is executed that resulted in an
arithmetic, numeric, string, conversion, or constraint error. This error mainly results from
programmer error or invalid data input.
4. ZERO_DIVIDE = raises exception WHEN dividing with zero.
What is XML?
Extensible Markup Language (XML) is a markup language that defines a set of rules for
encoding documents in a format that is both human-readable and machine-readable. The
design goals of XML focus on simplicity, generality, and usability across the Internet. It is
a textual data format with strong support via Unicode for different human languages.
Although the design of XML focuses on documents, the language is widely used for the
representation of arbitrary data structures such as those used in web services.
OUTPUT:
Output:
Basically XML above does not do anything. XML is just information wrapped in tags.
Users must require a piece of software to send, receive, store, or display it.
XML makes web development User Friendly : Many computer systems contain data in
incompatible formats. Exchanging data between incompatible systems or upgraded systems
is a time-consuming task for web developers. Large amounts of data must be converted,
and incompatible data is often lost. XML stores data in plain text format. This provides a
software- and hardware-independent way of storing, transporting, and sharing data.
XML is Extensible: XML applications will work as expected even if data is edited i.e.
added or removed.
Example :The above note is edited into a newer version by adding date tag and hour tag ,
and by removing the heading tag.
New Version
output:
Note
To:Raj
From:Ravi
Date:2015-09-01 08:30
Meeting at 8am
What is SQLite
SQLite is a software library that provides a relational database management system. The
lite in SQLite means lightweight in terms of setup, database administration, and required
resources.
Self-contained
Serverless
Zero-configuration
Transactional
Serverless
An RDBMS such as MySQL, PostgreSQL, etc., normally requires a separate server process
to run. The applications that want to access the database server use TCP/IP protocol to send
and receive requests. This is called client/server architecture.
SQLite database is integrated with the application that accesses the database. The
applications interact with the SQLite database reading and writing directly from the database
files stored on disk.
Self-Contained
SQLite is self-contained, which means it requires minimal support from the operating system
or external library. This makes SQLite usable in any environment, especially in embedded
devices like iPhones, Android phones, game consoles, handheld media players, etc.
SQLite is developed using ANSI-C. The source code is available as a big sqlite3.c and its
header file sqlite3.h. If you want to develop an application that uses SQLite, you drop these
files into your project and compile them with your code.
Because of the serverless architecture, you don’t need to “install” SQLite before using it. No
server process needs to be configured, started, and stopped.
Transactional
All transactions in SQLite are fully ACID-compliant, which means all queries and changes
are Atomic, Consistent, Isolated, and Durable.
In other words, all changes within a transaction take place completely or not at all even when
an unexpected situation like an application crash, power failure, or operating system crash
occurs.
SQLite uses dynamic types for tables. It means you can store any value in any column,
regardless of the data type.
SQLite allows a single database connection to access multiple database files simultaneously.
This brings many nice features like joining tables in different databases or copying data
between databases in a single command.
SQLite is capable of creating in-memory databases that are very fast to work with.
PDO refers to PHP Data Object, which is a PHP extension that defines a lightweight and
consistent interface for accessing a database in PHP. It is a set of PHP extensions which
provide a core PDO class and database-specific driver. Each database driver can expose
database-specific features as a regular extension function that implements the PDO interface.
PDO mainly focuses on data access abstraction rather than database abstraction. It
provides data-access abstraction layer, which means, regardless of which database we are
using, we have to use the same functions provided by that database to issue queries and fetch
data. PDO does not provide data abstraction, as it does not rewrite the SQL or emulate
missing features.
Advantage of PDO
PDO provides various ways to work with objects and retrieves prepared statements that make
work much easier. It is a database access tool in PHP through which we enable uniform
access across several databases.
PDO allows comparatively seamless switching between different databases and platforms,
which can be easily done by changing the connection string. It does not support database-
specific syntaxes.
o Database support
The PDO extension can access any database which is written for PDO driver. There
are several PDO drivers available which are used for FreeTDS, Microsoft SQL
Server, Sybase, IBM DB2, Oracle Call Interface, Firebird/Interbase 6,
and PostgreSQL databases, among many more.
The drivers are not available in every system automatically, so we have to find our
available drivers and add ones when we need them.
o Database connecting
There are different syntaxes available to establish the database connection. These
syntaxes depend on specific databases. While using PDO, operations must be
wrapped in try/catch blocks and utilize the exception technique.
PDO is the native database driver. There are some benefits of using PDO that are given
below:
XML | Syntax
XML syntax rule which is used while writing an XML document or an XML
application. It is a very simple and straight forward to learn and code. Below is a
complete XML document to discuss each component in detail.
What is XML?
XML documents must contain one root element that is the parent of all other elements:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The XML prolog is optional. If it exists, it must come first in the document.
XML documents can contain international characters, like Norwegian øæå or French êèé.
To avoid errors, you should specify the encoding used, or save your XML files as UTF-8.
In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
<p>This is a paragraph.</p>
<br />
XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.
Opening and closing tags must be written with the same case:
<message>This is correct</message>
Opening and closing tags" are often referred to as "Start and end tags". Use whatever you
prefer. It is exactly the same thing.
Comments in XML
XML
XML
<to>Students</to>
<from>Teacher</from>
<subject>Regarding assignment submission</subject>
In the first example the Root element is <message> and all the remaining elements
<to>, <from> etc is the child element and reside within the root element.
It is case sensitive.
The XML element should have a closing element for example <text category =
“message”>Hi</text> is correct but <text category = “message”>Hi is not correct
because it does not contain the closing element and it will throw an error and vice-
versa.
The elements in XML should be nested properly otherwise it will throw an error. For
example <to><from>Geeks</from></to> is nested correctly
but <to><from>Geeks</to></from> is wrong because if <from> is opened inside the
<to> element then this should also end inside of the </to> element.
It is also case sensitive i.e, the starting and closing element should be in the same case.
For example <to>….</to> is correct but <to>…..</To> is not correct and it will throw
an error.
The XML attribute is having two part one is Name and other is its value. It resides
inside of the opening of an XML element. For example: <text category =
“message”>All students will have to submit the assignment by tomorrow.</text> Here
category is the attribute name and message is its value and the attribute value should
either be in a single quotation or in double quotation otherwise it will throw an error.
The Attribute Name is written without any quotation.
The XML attribute is also case sensitive.
An XML element can have multiple attributes but can not have the same attribute
names in the same element. For example: <text category =”message” purpose =
“greet”>GeeksforGeeks</text> Above attributes is correct because of having multiple
attributes with the different attribute name. <text category =”message” category =
“greet”>GeeksforGeeks</text> Above attribute is wrong because of having the same
attribute name in a single element.
XML Comments: Correct syntax for writing XML comments are: <!– It is comment
section –>
DOM
The Document Object Model (DOM) defines a standard for accessing and manipulating
documents:
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that
allows programs and scripts to dynamically access and update the content, structure, and style
of a document."
The HTML DOM defines a standard way for accessing and manipulating HTML documents.
It presents an HTML document as a tree-structure.
The XML DOM defines a standard way for accessing and manipulating XML documents. It
presents an XML document as a tree-structure.
The DOM extension allows operations on XML and HTML documents through the DOM
API with PHP.
<!DOCTYPE html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').innerHTML = 'Hello World!'">Click
Me!
</body>
</html>
OUTPUT
This is a Heading
Books.xml
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
This code retrieves the text value of the first <title> element in an XML document:
txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;