InternetProgramming Databases
InternetProgramming Databases
InternetProgramming Databases
▪ access a database
▪ A database enables you to efficiently store, search, sort, and retrieve data.
▪ MySQL uses SQL (Structured Query Language), the standard database query language
worldwide.
• PHP is a powerful tool for making dynamic and interactive Web pages e.g feedback
forms, guestbook etc.
o Logical Tier – Web Server (Apache, IIS, Websphere etc.); Scripting Languages (PHP,
Perl etc.), Programming Languages (Java, C, C# etc), Application Frameworks (Ruby
on Rails etc.)
o Data Tier – Database Management System (DBMS) (Oracle, MySQL, SQL Server, DB2
etc.), XMLDB
2. The web server receives the request for results.php, retrieves the file, and passes
it to the PHP engine for processing.
3. The PHP engine begins parsing the script. Inside the script is a command to connect
to the database and execute a query (perform the search for books). PHP opens a
connection to the MySQL server and sends on the appropriate query.
5. The PHP engine finishes running the script, which usually involves formatting the query
results nicely in HTML. It then returns the resulting HTML to the web server.
6. The web server passes the HTML back to the browser, where the user can see the list of
books she requested.
server:
o PHP Data Objects (PDO) - PHP Data Objects, or PDO, is a database abstraction
layer that provides a consistent API regardless of the type of database server. In
theory, it allows for the switch of the database server, from say Firebird to MySQL,
with only minor changes to the PHP code.
** If using MySQL versions 4.1.3 or later it is strongly recommended that the mysqli
extension is used.
• Assign the return value from the mysqli_connect() function to a variable that can use
to access the database in the script
• The host argument specifies the host name where the MySQL database server is
installed
• The user and password arguments specify a MySQL account name and password
• You can optionally select the database when connecting.
▪ The mysqli_errno() function returns the error code from the last attempted MySQL
function call or 0 if no error occurred
▪ The mysqli_error() — Returns the text of the error message from previous MySQL
operation
▪ The mysqli_errno() and mysqli_error() functions return the results of the previous
mysqli*() function
mysqli_select_db(connection, database);
• A result pointer is a special type of variable thatrefers to the currently selected row
in a resultset
– The mysqli_query() function returns a value of FALSE for any SQL statements that
fail, regardless of whether they return results
▪ To add multiple records to a database, use the LOAD DATA statement with the name
of the local text file containing the records you want to add
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
▪ The SET keyword specifies the value to assign to the fields in the records that match
▪ To delete records in a table, use the DELETE statement with the mysqli_query()
function
▪ The mysqli_fetch_assoc() function returns the fields in the current row of a resultset
into an associative array and moves the result pointer to the next row
▪ With queries that return results (SELECT queries), use the mysqli_num_rows()
function to find the number of records returned from the query
▪ With queries that modify tables but do not return results (INSERT, UPDATE, and
DELETE queries), use the mysqli_affected_rows() function to determine the number
of affected rows
▪ For queries that add or update records, or alter a table’s structure, use the
mysqli_info() function to return information about the query
▪ The mysqli_info() function returns the number of operations for various types of
actions, depending on the type of query
▪ The mysqli_info() function returns information about the last query that was
executed on the database connection
▪ The mysqli_info() function returns information about queries that match one of the
following formats:
▪ – INSERT INTO...SELECT...
▪ – INSERT INTO...VALUES (...),(...),(...)
▪ – LOAD DATA INFILE ...
▪ – ALTER TABLE ...
▪ – UPDATE
▪ For any queries that do not match one of these formats, the mysql_info() function
returns an empty string
45
46
47
Closing Query Results
▪ When you are finished working with query results retrieved with the
mysqli_query() function, use the mysqli_free_result() function to close the
resultset.
48
Query Result Information
49
▪ <?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Name,City FROM Persons ORDER BY Name";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf(“Your query returned %d rows.\n",$rowcount);
mysqli_close($con);
?>
50
Escaping Dangerous characters
▪ Makes a string safe to use by searching and escaping all dangerous characters(eg. `
or % ) that could lead to SQL errors and injection attacks
51
Source : https://www.w3schools.com/
52
Extra..
▪ SQL Injection Attacks
▪ MySQLi OOP-style syntax vs. MySQLi procedural-style syntax
53
Summary
▪ mysqli_connect() function : opens a connection to a MySQL database server
▪ mysqli_close() function : closes a database connection
▪ mysqli_errno() function : returns the error code from the last attempted MySQL
function call or zero if no error occurred
▪ mysqli_error() function : returns the error message from the last attempted
MySQL function call or an empty string if no error occurred
▪ mysqli_create_db() function : to create a new database
▪ mysqli_select_db() function : selects a database
▪ mysqli_drop_db() function : to delete a database
▪ mysqli_query() function : sends SQL statements to MySQL
54
Summary
▪ A result pointer is a special type of variable that refers to the currently selected
row in a resultset
▪ CREATE TABLE statement with the mysqli_query() function to create a table
▪ PRIMARY KEY clause indicates a field or fields that will be used as a referential
index for the table
▪ The AUTO_INCREMENT clause creates a field that is automatically updated with
the next sequential value for that column
▪ The NOT NULL clause creates a field that must contain data
▪ mysqli_info() function : returns the number of operations for various types of
actions, depending on the type of query.
▪ mysqli_fetch_row() function : returns the fields in the current row of a resultset
into an indexed array and moves the result pointer to the next row
55
Summary
▪ mysqli_fetch_assoc() function: returns the fields in the current row of a resultset
into an associative array and moves the result pointer to the next row
56
References
▪ https://alexwebdevelop.com/php-with-mysql/
▪ http://zetcode.com/php/mysqli/
▪ https://www.math.ucla.edu/~virtanen/40a.1.15s/schedule/lec21.pdf
▪ https://www.studocu.com/en/document/swinburne-university-of-technology/web-
application-development/lecture-notes/my-sql-lecture-notes-4/965610/view
▪ https://www.tjscott.net/websecurity/php.mysql.tutorial.pdf
▪ https://www.w3schools.com/