Internet programming, Slides of Information Integration

Internet programming courses by yetbarekworku

Typology: Slides

2023/2024

Uploaded on 11/25/2024

tolerate-man
tolerate-man šŸ‡ŖšŸ‡¹

5 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter : Three
Manipulating Databases with PHP
•
•
•
One of the reasons for PHP’s popularity as a Web scripting language is
its (cross-platform, compatible, scalability, Allows for various DBs ….
etc)
Allows for various DB formats (Microsoft SQL Server, IBM DB2,
PostgreSQL, MySQL, and Oracle. )
Makes it easy for Web developers to create Web applications quickly
and efficiently.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download Internet programming and more Slides Information Integration in PDF only on Docsity!

Chapter : Three

Manipulating Databases with PHP

One of the reasons for PHP’s popularity as a Web scripting language is its (cross-platform, compatible, scalability, Allows for various DBs …. etc) Allows for various DB formats (Microsoft SQL Server, IBM DB2, PostgreSQL, MySQL, and Oracle. ) Makes it easy for Web developers to create Web applications quickly and efficiently.

Database Access in PHP

Database: is a separate application that stores a collection of data. Table: is a set of rows and columns. It represents a single concept such as products. Column: a set of data of single data type. Ex. FirstName, LastName, Row: single record of data. Ex. ā€œAbebeā€, ā€œKebedeā€, Field: is the intersection of a row and a column. Ex. FirstName: ā€ Abebeā€ Redundancy: Storing data twice, redundantly to make the system

MySQL Database:

  • • • • • • MySQL is becoming so popular because of many good reasons. MySQL works on many operating systems and with many languages including PHP, PERL, C, C++, JAVA, etc. MySQL works very quickly and works well even with large data sets. MySQL is very friendly to PHP, the most appreciated language for web development. MySQL supports large databases, up to 50 million rows or more in a table. The default file size limit for a table is 4GB, but you can increase this (if your operating system can handle it) to a theoretical limit of 8 million terabytes (TB).

Database Interactions

PHP database interactions in five steps:

Create a database connection

Perform Database query

Use returned data if any

Release returned data

Close database connection

Cont.…

The common function in PHP that uses for server connection is

mysql_connect( ) or mysqli_connect() function.

This function has the following syntax:- mysqli_connect ("

hostname", "user", "pass") to connect with MySQL server.

PHP provides mysqli_connect function to open a database

connection. This function can take up to five parameters and

returns a MySQL link identifier on success, or FALSE on failure.

The five parameters are the three above and the two below options.

Cont.…

new_link Optional - If a second call is made to mysqli_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned. client_flags Optional - A combination of the following constants: MYSQL_CLIENT_SSL - Use SSL encryption MYSQL_CLIENT_COMPRESS - Use compression protocol MYSQL_CLIENT_IGNORE_SPACE - Allow space after function names MYSQL_CLIENT_INTERACTIVE - Allow interactive timeout seconds of inactivity before closing the connection

Note: There are more available parameters, but the ones listed above are

the most important.

Databaseconnection.php

Closing a DB connection

You can disconnect from MySQL database anytime using another PHP function mysqli_close(). This function takes a single parameter which is a connection returned by mysqli_connect() function. Syntax:

mysql_close ( resource $link_identifier );

mysqli_close($conn); or mysql_close($conn);

This function returns true if it closes connection successfully otherwise it returns false.

Creating the working Database

After establishing a MySQL connection with the code above, you then need to choose which database you will be using with this connection. If the database you are looking to work on is not available, you can create it using mysqli_query() or mysql_query() function together with CREATE command followed by database name.

mysqli_query function can take two parameters and returns TRUE on

success or FALSE on failure. The parameters are:- sqli and connection.

Cont..

The syntax of the function is:-

mysql_query(sql, connection variable); or

mysqli_query(connection variable,sql);

To create a database uses the following sql syntax:

CREATE DATABASE database_name

mysql_query ("create database testā€,$connection): told MySQL to

create a database called test.

Creating the working Database

Creating the working Database

Cont.…

There are also functions in PHP which have different purposes. For

instance,

mysql_select_db("database name") or mysqli_select_db(ā€œconnectionā€

,"database name") : Equivalent to the MySQL command USE; makes

the selected database the active one.

mysqli_query("query"): Used to send any type of MySQL command

to the server.

mysqli_fetch_rows("results variable from query"): Used to return a

mysqli_affected_rows():Print out affected rows from different

queries:

mysql_fetch_array("results variable from query"): Used to return

several rows of the entire results of a database query.

mysql_free_result(ā€œresult variable from queryā€): Used to release the

returned results.

mysql_error(): Shows the error message that has been returned

directly from the MySQL server.

Cont.…