Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Lecture Note Chapter 7 - Databases and PHP

Rating
-
Sold
-
Pages
6
Uploaded on
20-03-2021
Written in
2020/2021

An intuitive brief note of the lecture content and the slides. I got 1.3 in the exam with this note and hope you will find it helpful for your review before the exam.

Institution
Course

Content preview

Ch1. The Internet
Ch2. The Internet protocol stack
Ch3. Web Servers
Ch4. HTML
Ch5. Apache and PHP
Ch6. Databases
Ch7. Databases and PHP
Ch8. Business Logic and JavaScript
Ch9. Security
Ch7. Databases and PHP
I. Execute SQL in PHP:
- Connect PHP code to DB to make it possible to store data in DB and read data from DB.
1. PHP data objects (PDO)
- Idea: Use SQL statements in PHP code to: store data in DB + read data from DB.
- PDO: a library that helps us access all kinds of DB from PHP.
- Connecting to DB before writing SQL queries:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "duskycars";
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
?>
(servername, username, password, dbname can be adjusted in the MariaDB setting)
→ Result: connection successful
- Now we are connected to DB. We can send SQL queries for example:
<?php
//copy and paste the connection code from the previous slide here
$stmt = $conn->query("SELECT * FROM customer;");
$result = $stmt->fetchAll();
foreach($result as &$customer){
echo $customer['id'] . " " . $customer['firstName'] . " " . $customer['lastName'];
echo "<br>";
}
?>
Explain:
$stmt = $conn->query("SELECT * FROM customer;");
This is how to create SQL queries in PHP: PHP actually send the query to DB to get the required
info. This info is stored in the $stmt variable.
The $stmt variable is an object containing all the data from the customer table. PDO creates this
object. BUT it’s not a table yet.
$result = $stmt->fetchAll();

, The fetchAll() is a method that PDO provides. It returns an array containing
all of the result set rows ➔ simply: It returns all the data that is stored in
the $stmt object in form of a table (that can access with PHP code).
foreach($result as &$customer){
echo $customer['id'] . " " . $customer['firstName'] . " " . $customer['lastName'];
echo "<br>";
}
The foreach loop goes through table $result one line at a time, every lines.
The current line is called $customer. This means each iteration of the loop, variable $customer
contains a different row from the table.
Keyword echo is like print command, returns the data as String → HTML code is written.
With the bracket e.g. ['firstName'] we can specify the which data of the row to execute echo
command. Here we choose ['id'], ['firstName'], and ['lastName']. We don’t choose column
[‘password'].

The <br> is line breaker. The . operator concatenates two strings.
2. HTML and PHP
Remember we can add HTML code to PHP page.
<html>
<head>
<title>Connect to MariaDB Server</title>
</head>
<body>
<h3>Customers:</h3>
//add PHP code in the body of the HTML doc
</body>
</html>


3. Inserting data to DB
Quite similar to requesting data:
<?php
//copy and paste the connection code here
$sql = "INSERT INTO customer (firstName, lastName, password) VALUES ('Test', 'User', 'pw123');";
$conn->exec($sql);
echo "New customer registered successfully.";
?>
BUT this is just static way to insert data.
We want to connect PHP with HTML forms for user to fill data in.
 Separate to HTML file and PHP code.

Connected book

Written for

Institution
Study
Course

Document information

Uploaded on
March 20, 2021
Number of pages
6
Written in
2020/2021
Type
Class notes
Professor(s)
Christian becker
Contains
Lecture 7 - databases and php

Subjects

$4.14
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sonphan

Also available in package deal

Get to know the seller

Seller avatar
sonphan Universität Mannheim
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
5 year
Number of followers
1
Documents
0
Last sold
5 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Trending documents

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions