Web Dev 3 Part 1
Web Dev 3 Part 1
Ashley Cahill
[email protected]
Recap
• Web Processing
• Web History
• PHP History
• Echo
• Variables
• Arithmetic Operations
• Forms
• Validation
Recap
• Exercise 1
• Discount Calculator
• Exercise 2
• Discount Calculator with Validation
Learning Outcomes
• History of MySQL.
• Connecting to a database.
• MySQL Workbench
• NetBeans
NetBeans Configuration
XAMPP Configuration
• Give it a meaningful name and set the Port to the Port your MySQL
Service is running in XAMPP
Setting Up a Database
Lab
• Create a Database “WebDev3_2024”
• Insert 1 record
USE WebDev3_2024;
Creating a Table
CREATE TABLE car_details
(
modelvarchar(233)NOT NULL,
PRIMARY KEY(id)
);
Inserting Data
$dsn = 'mysql:host=localhost;dbname=WebDev3_2024';
$username = 'root';
$password = '';
$dsn='mysql:host=127.0.0.1:337;dbname=WebDev3_2024';
$username = 'root';
$password = '';
• Remember … clicking the green arrow in NetBeans will run every file
you have open
• Right click on your file and select “Run File” to run the individual file
Lab
<?php
//$dsn='mysql:host=localhost;dbname=WebDev3_2024';
$dsn='mysql:host=127.0.0.1:3307;dbname=WebDev3_2024';
$username = 'root';
$password = '';
?>
Programming
• Programming takes perseverance
• Also, read the error messages. PHP tries to tell you the
exact cause (file, line number etc.)
• Don’t just give up!!
Common Issues With Connect
• $dsn='mysql:host=127.0.0.1:3307;dbname=WebDev3_2024’;
Check Port?
Is XAMPP running on
3306/3307/3309?!?!
Common Issues With Connect
• $dsn='mysql:host=127.0.0.1.3307;dbname=WebDev3_2024';
• $password = '';
try {
try {
$error_message = $e->getMessage();
}
PDO Exception Handling
• How to handle a PDO exception
try {
$error_message = $e->getMessage();
echo '<p>An error occurred while connecting to the database: ‘ . $error_message . '</p>';
}
Lab
• Add exception handling to the php file used to connect to the database
• If error display “An error has occurred” and then display the actual PDO error
message