Complete PHP and SQL Cheatsheet
PHP Basics
PHP Basics:
-----------
- PHP tags: <?php ... ?>
- Variables: $var = value;
- Data types: string, int, float, bool, array, object, NULL
- Constants: define("NAME", "value");
- Operators: + - * / % . (concatenation), ==, ===, !=, !==, <, >, <=, >=
Control Structures:
- if, else, elseif
- switch
- while, do...while, for, foreach
Functions:
function name($param1, $param2 = default) {
return $result;
}
Complete PHP and SQL Cheatsheet
PHP OOP
PHP Object-Oriented Programming:
--------------------------------
class ClassName {
public $property;
function __construct($value) {
$this->property = $value;
}
public function method() {
return $this->property;
}
}
- Inheritance: class Child extends Parent {}
- Visibility: public, private, protected
- Static members: static keyword
- Interfaces, Traits, Abstract classes supported
Complete PHP and SQL Cheatsheet
PHP Sessions & Cookies
PHP Sessions & Cookies:
------------------------
- session_start();
- $_SESSION["var"] = value;
- session_destroy();
Cookies:
setcookie("name", "value", time()+3600);
$_COOKIE["name"];
Complete PHP and SQL Cheatsheet
PHP Miscellaneous
PHP Miscellaneous:
------------------
- Include files: include, include_once, require, require_once
- File handling: fopen(), fread(), fwrite(), fclose()
- Error handling: try { } catch (Exception $e) { }
- Superglobals: $_GET, $_POST, $_SERVER, $_FILES, $_SESSION, $_COOKIE, $_ENV, $_REQUEST,
$GLOBALS
Complete PHP and SQL Cheatsheet
SQL Basics
SQL Basics:
-----------
- SELECT * FROM table;
- SELECT column1, column2 FROM table WHERE condition;
- INSERT INTO table (col1, col2) VALUES (val1, val2);
- UPDATE table SET col = val WHERE condition;
- DELETE FROM table WHERE condition;
Clauses:
- WHERE, ORDER BY, GROUP BY, HAVING, LIMIT, OFFSET
Complete PHP and SQL Cheatsheet
SQL Joins
SQL Joins:
----------
- INNER JOIN: Selects records with matching values in both tables.
- LEFT JOIN: All from left + matching from right.
- RIGHT JOIN: All from right + matching from left.
- FULL JOIN: All records when there is a match in one of the tables.
Example:
SELECT a.name, b.salary FROM employees a
JOIN salaries b ON a.id = b.emp_id;
Complete PHP and SQL Cheatsheet
SQL Functions & Advanced Queries
SQL Functions and Advanced:
---------------------------
- COUNT(), SUM(), AVG(), MIN(), MAX()
- DISTINCT: SELECT DISTINCT col FROM table;
- LIKE: pattern matching (% and _)
- IN, BETWEEN, IS NULL
Subqueries:
SELECT name FROM employees WHERE dept_id IN (SELECT id FROM departments WHERE location =
'NY');
Complete PHP and SQL Cheatsheet
SQL Transactions
SQL Transactions:
-----------------
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT; -- or ROLLBACK;
Used for ensuring atomic operations.
Complete PHP and SQL Cheatsheet
SQL Permissions & User Management
SQL Permissions & Users:
------------------------
- CREATE USER 'user'@'host' IDENTIFIED BY 'password';
- GRANT SELECT, INSERT ON db.* TO 'user'@'host';
- REVOKE ALL PRIVILEGES ON db.* FROM 'user'@'host';
- DROP USER 'user'@'host';