Skip to content

markbuckle/PHP-MySQL-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP/MySQL-tutorial

Extensions

  1. PHP Intelephense
  2. Live Server
  3. PHP Server

Access your localhost by typing localhost into your webbrowser. You can access your VS code project by typing localhost/[enter your projects folder name]. Add the Live Server extension on Google chrome if you want your VS Code updates to happen live.

Shortcuts

Type "!" and press tab for boiler plate code.

All PHP code is contained within . You can also write html, css and javascript alongside PHP.

Variables

A variable is a reusable container that holds dataincludng strings, integers, floats and booleans

$_GET and $_POST are two special php variables used to collect data from an HTML form i.e.

.

$_GET means data is appended to the url.

  • It is not secure; don't use any sensitive informaiton.
  • There is a character limit
  • bookmarks are possible w/values.
  • Get requests can be cached
  • Get requests are better for a search page.
  • $_POST means the data is packaged inside the body of the HTTP request.

  • It is more secure
  • there is no data limit
  • you cannot bookmark
  • Post requests are better for submitting credentials.
  • Sanitize & Validate Input

    Sanitizing and validating user input is always a good idea in case the rare user decides to input malicious content.

    You can sanitize your code with functions like:

  • $username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_SPECIAL_CHARS);
  • $age = filter_input(INPUT_POST, "age", FILTER_SANITIZE_NUMBER_INT);
  • $email = filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL);
  • You can validate your code with tests like:

  • $age = filter_input(INPUT_POST, "age", FILTER_VALIDATE_INT);
  • $email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
  • More details on this can be found at 2:44:30

    Cookies

    To print values from cookies, do as follows:

    setCookie(key, value, time() + time til expiry, path);

    foreach($_COOKIE as $key => $value){ echo"{$key} = {value}
    "; }

    Sessions

    A session is a super global varable (SGB) used to store information on a user to be used across multiple pages. A user is assigned a session-id ex. login credentials. Any login websites i.e. Facebook use sessions.

    Place session_start(); in a php tag before the rest of your code.

    You can then create named value pairs within our SGB by using i.e. $_SESSION["username"] = "Buckle23" then echo $_SESSION["username"];

    Server

    The server SGB contains headers, paths and script locations. The entries in this array are created by the web server. It shows nearly everything you need to know about the current web page env.

    To access the server SGB type $_SERVER["PHP_SELF"];

    Password Hashing

    Password hasing is transforming sensitive data (password) into letters, numbers, and/or symbols via a mathematical process (similar to encryption). It hides the original data from 3rd parties.

    Use the password_has($password, PASSWORD_DEFAULT); function.

    PHP connect to MySQL

    There's two popular ways to connect PHP to MySQL:

    1. MySQLi Extension
    2. PDO (PHP Data Objects) - many developers use PDO's because they can connect to 12 or more database types.

    For the purpose of this tutorial we will just be using the MySQLi extension

    Go to http://localhost/phpmyadmin/

    This url allows to you to configure your database. You can make SQL queries, monitor the status, export/import data, etc.

    Create a db for this tutorial called tutorialdb

    Go to the User Accounts Overview at http://localhost/phpmyadmin/index.php?route=/server/privileges&viewing_mode=server

    Create a file called database.php

    You should be able to view the output at http://localhost/tutorial-website/database.php

    To create a table like this:

    Tutorial video

    PHP Full Course

    About

    No description, website, or topics provided.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published

    Languages