PHP SQL Cheatsheet Complete
PHP SQL Cheatsheet Complete
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
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
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;