0% found this document useful (0 votes)
1 views28 pages

Lecture 7-Using Database (2)

This document outlines the process of developing, setting up, and connecting to a MySQL database for web development. It includes instructions on naming database elements, accessing MySQL, and using PHP to connect to the database. Additionally, it covers data types, column properties, and executing queries to manage user data.

Uploaded by

Pele Mpele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views28 pages

Lecture 7-Using Database (2)

This document outlines the process of developing, setting up, and connecting to a MySQL database for web development. It includes instructions on naming database elements, accessing MySQL, and using PHP to connect to the database. Additionally, it covers data types, column properties, and executing queries to manage user data.

Uploaded by

Pele Mpele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

DYNAMIC WEB DEVELOPMENT

WEEK 7.A: DEVELOP, SETUP AND CONNECT TO A


DATABASE

Dr. Basel Almourad


GET READY
 For this Lecture you have to do Tutorial 5
Part 1

 Start Apache Web Server & MySQL


Server (using XAMPP control panel)
GOALS
 Learn how to develop a new database, setup a database in
MySQL, and connect a web page to the created database.

OUTLINE
1. Design and develop a database
2. Access MySQL
3. Setup a database in MySQL
4. Connect to a database using PHP
5. Q & A
DEVELOP A DATABASE
 Naming Database elements
 database name:
o Just make sure the name is unique within your MySQL server
o example: we will use registrations
 tables’ names
o need to be unique within this database
o example: we will use users
 columns’ names & data type
o depends on the data to be stored
o example: see next slide

-------------- chose meaningful names for all items -----------


DEVELOP A DATABASE
As example, we will be using the columns below for the
users table
DEVELOP A DATABASE
 How to chose a column type?
 Step 1: identify the high-level type; e.g. number, text or date/time
 Step 2: chose the most appropriate sub-type, depending on the used
database
 Step 3: add any restriction, e.g. maximum number of characters

Step 1 Step 2 Step 3


 MySQL data types: examples
DEVELOP A DATABASE
 CHAR vs. VARCHAR
 Both store strings
 CHAR stored with fixed length, VARCHAR stored
with variable length:
 e.g.
 The word CAT stored in CHAR(10) requires 10
bytes, but requires 4 bytes if VARCHAR(10)

 Which one to use?


 If variable with fixed number of bytes (e.g.
emirate ID)  use CHAR
 Otherwise, use VARCHAR
DEVELOP A DATABASE
CHAR vs. VARCHAR vs. TEXT
 CHAR : the fastest to store and retrieve
 Maximum 255 Character
 VARCHAR: can be slower to store and retrieve
 TEXT: a character Binary Large Object (BLOB)
 can hold a variable amount of data
 TEXT columns cannot have DEFAULT values.
 Can store files, images, etc.
DEVELOP A DATABASE
ENUM and SET
 ENUM: string object with value from a list of permitted
values enumerated explicitly at table creation time

 SET: Like ENUM except that each column can have more
than one of several possible values
DEVELOP A DATABASE
Other Column Properties
 NOT NULL: force a column to have a value
 a primary key column should always be NOT NULL
 Primary key:
• must always have a value
• value must never change
• value must be unique for each record
 Foreign key:
 primary key in one table is often linked as a foreign key
in another
 AUTO_INCREMENT:
 Default starting value 1
 Auto-increments by 1 for each new record
ACCESSING MYSQL SERVER
 Several ways to access MySQL
 e.g.
 mysql client
 MySQL Workbench
 Web-based phpMyAdmin
 PHP code
ACCESSING
MYSQL SERVER
 MySQL Workbench
• To lunch MySQL
Workbench
 from start menu, search for
mysql
 Then click MySQL 2
Workbench CE

1
ACCESSING MYSQL SERVER
 Web-based phpMyAdmin
You can access the MySQL Server via the PHP Admin
Interface
ACCESSING MYSQL SERVER
Practice :
• Create the database and the table with the
specified columns, in MySQL server
• Use phpMyAdmin
Tutorial 5 – Part 1
• Add some users to the table

Database: registrations

Table: users

AUTO_INCREMENT
ACCESSING MYSQL SERVER
 PHP Code
o This is what we will use from a web page

 Connecting to MySQL
$dbc = mysqli_connect (hostname,
➝ username, password, db_name);

More on next chapters …..


CONNECTING TO MYSQL
$dbc = mysqli_connect (hostname, username, password, db_name);
mysqli_set_charset($dbc, 'utf8’); // not necessary but useful

Or better version …

$dbc = @mysqli_connect (hostname, username, password, db_name)


OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

(@). prevents the PHP error from being


displayed in the Web browser. This is
preferable, as the error will be handled
by the OR die( ) clause.
CONNECTION ERRORS
PROTECTING ACCESS

Best to store the MySQL connection


script outside of the Web root
directory.
EXECUTING SIMPLE
QUERIES

$sql = "SELECT id, firstname, lastname FROM user";


$r = mysqli_query($dbc, $sql);

if ($r) { // Worked!
// Do whatever.
} else {
echo mysqli_error($dbc);
}

mysqli_close($dbc);
COUNTING RETURNED
RECORDS
$num = mysqli_num_rows($r);
RETRIEVING QUERY
RESULTS
while ($row = mysqli_fetch_array($r)) {
// Do something with $row.
}

mysqli_free_result ($r);
mysqli_free_result
removes the overhead (memory)
taken by $r. It’s an optional step, since PHP
will automatically free up the resources
at the end of a script, but—like using
mysqli_close( )—it does make for good
programming form
EXAMPLE
$num = mysqli_num_rows($r); // Count the number of returned
rows:
if ($num > 0) { // If it ran OK, display the records.
// Print how many users there are:
echo "<p>There are currently $num registered users.</p>\n";
// Fetch and print all the records:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo $row['name'] . ' ' . $row['dr'] . '</br>';
}
mysqli_free_result ($r); // Free up the resources.
} else { // If no records were returned.
echo '<p class="error">There are currently no registered users.</p>'
RETRIEVING QUERY
RESULTS

Constant Example
MYSQLI_ASSOC $row['column']
MYSQLI_NUM $row[0]
MYSQLI_BOTH $row['column'] or $row[0]

while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {


// Do something with $row[0], $row[1], etc.
}
view_users1.php view_users2.php
COUNTING AFFECTED
RECORDS

$num = mysqli_affected_rows($dbc);

$q = "UPDATE users SET pass=SHA1('$np') WHERE user_id=$row[0]";


$r = @mysqli_query($dbc, $q);
if (mysqli_affected_rows($dbc) == 1) {
echo '<h1>Thank you!</h1>
<p>Your password has been updated. In Chapter 12 you will actually
be able to log in!</p><p><br /></p>’;

mysqli_affected_rows
Returns the number of records affected by
an INSERT, UPDATE, or DELETE query.
ANY QUESTIONS?

31
REFERENCES AND MORE READING
• Book
• Chapter 4: Introduction to MySQL
• Chapter 9: Using PHP with MySQL

• W3School
• https://www.w3schools.com/php/php_mysql_intro.asp

32

You might also like