100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Lecture Note Chapter 7 - Databases and PHP $3.25   Add to cart

Class notes

Lecture Note Chapter 7 - Databases and PHP

 2 views  0 purchase
  • Course
  • Institution
  • Book

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.

Preview 2 out of 6  pages

  • March 20, 2021
  • 6
  • 2020/2021
  • Class notes
  • Christian becker
  • Lecture 7 - databases and php
avatar-seller
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.

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller sonphan. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $3.25. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

67232 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$3.25
  • (0)
  Add to cart