PHP Notes 1
PHP Notes 1
Reference:
https://www.youtube.com/watch?v=pWG7ajC_OVo&list=PL4cUxeGkcC9gksOX3Kd9KPo-O68nc
T05o
Introduction to PHP
Why PHP?
● Huge & active community for support
● Very popular (WordPress, Drupal, Magento)
Figure 1.0
What You’ll Learn
● PHP basics & creating PHP files
● Rendering dynamic content to HTML templates
● How to communicate with MySQL databases
● Cookies & Sessions
● PHP objects & classes
Conditional Statements
● Conditional statements (if, else if etc); The conditionals are used to branch our
code depending on whether a certain condition is true or false.
Variable Scope
● Local variables are declared within a function and have its scope only in that
particular function.
● In PHP global variables must be declared global inside a function if they are
going to be used in that function.
● Global variables refer to any variable that is defined outside of the function.
Include & Require
● Two (2) functions: include & require - used to import other PHP files
● Include function continues with the code even if there is an error while the
require function does not continue with the code when it encounters an error.
Figure 1.2 - Form Validation with PHP Filter and Regex(Output #2)
Showing Errors
● To display errors on a web form, as well as persist data that a user has
previously entered.
● Reference:
https://github.com/iamshaunjp/php-mysql-tutorial/blob/lesson-21/add.php
Figure 1.0 - Checking for Error with Array_filter() Function (Input #1)
Figure 1.1 - Checking for Error with Array_filter() Function (Output #1)
Figure 1.2 - Passed Validation and Redirecting to index.php using Header() Function
MySQL Introduction
● MySQL is used to store data in.
● Relational database management system
● We use SQL to communicate with the database from PHP
● SQL = Structured Query Language
● Foreign Key is used to link the tables together
● Can contain several tables
● Each table stores a particular model of data (ex. Pizzas, Users)
● Each row represents a single record (ex. a Single Pizza)
● Each column represents a property of that record
● We use SQL to communicate with the database from PHP code
Figure 1.3 - Error when Something Wrong with the Input in Mysqli_connect() Function
Getting Data From a Database
● Use SQL to select (get) some data from a database.
● Process: (1) construct the MySQL query, and then (2) make the query, and then
(3) fetch the results from that query, and then (4) free result from memory, and
then (5) close the connection
● The mysqli_query() function performs a query against a database.
Syntax: mysqli_query(connection, query, resultmode)
● The mysqli_fetch_all() function fetches all result rows and returns the result-set
as an associative array, a numeric array, or both.
Syntax: mysqli_fetch_all(result, resulttype)
● Optional. Specifies what type of array that should be produced. Can be one of
the following values:
○ MYSQLI_ASSOC
○ MYSQLI_NUM (this is default)
○ MYSQLI_BOTH
● The print_r() function prints the information about a variable in a more
human-readable way.
● The mysqli_free_result() function frees the memory associated with the result.
● The mysqli_close() function closes a previously opened database connection.
Figure 1.0 - Getting Data from a Database with PHP
Figure 1.1 - Output from Getting Data from Database with Displayed Data on Browser
Figure 1.2 - Getting Data from a Database Process
Deleting a Record
● Delete a record from the MySQL database.
● Use a form for delete which is going to contain one hidden input containing the ID
of the data (or in this case the pizza) that we would like to delete so that will be
the value of the import.
● Then we're going to have a submit button which will say delete which is going to
make a post request, take that value the ID from the hidden input field.
● When we run PHP we're going to detect if the submit button was pressed by
using the isset() function with POST method.
● Use SQL DELETE statement to delete existing records in a table.
● Use mysqli_real_escape_string() function when deleting data from user to host to
take out harmful code the user might inject.
Figure 1.0 - Deleting a Record
Figure 1.1 - Deleting a Record (Output)
Finishing Touches on the Design
● Improve CSS code, add images, and edit text style