Lecture 7-Using Database (2)
Lecture 7-Using Database (2)
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
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);
Or better version …
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]
$num = mysqli_affected_rows($dbc);
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