Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
College aantekeningen

Lecture Note Chapter 7 - Databases and PHP

Beoordeling
-
Verkocht
-
Pagina's
6
Geüpload op
20-03-2021
Geschreven 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.

Instelling
Vak

Voorbeeld van de inhoud

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.

Gekoppeld boek

Geschreven voor

Instelling
Studie
Vak

Documentinformatie

Geüpload op
20 maart 2021
Aantal pagina's
6
Geschreven in
2020/2021
Type
College aantekeningen
Docent(en)
Christian becker
Bevat
Lecture 7 - databases and php

Onderwerpen

€3,49
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kan je een ander document kiezen. Je kan het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
sonphan

Ook beschikbaar in voordeelbundel

Maak kennis met de verkoper

Seller avatar
sonphan Universität Mannheim
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
1
Lid sinds
5 jaar
Aantal volgers
1
Documenten
0
Laatst verkocht
5 jaar geleden

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Populaire documenten

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Veelgestelde vragen