0% found this document useful (0 votes)
3 views9 pages

PHP SQL Cheatsheet Complete

This document is a comprehensive cheatsheet covering PHP and SQL basics, including syntax, control structures, object-oriented programming, sessions, cookies, and file handling in PHP. It also details SQL operations such as basic queries, joins, functions, transactions, and user management. The information is structured in sections for easy reference and includes examples for clarity.

Uploaded by

Koletto GALY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

PHP SQL Cheatsheet Complete

This document is a comprehensive cheatsheet covering PHP and SQL basics, including syntax, control structures, object-oriented programming, sessions, cookies, and file handling in PHP. It also details SQL operations such as basic queries, joins, functions, transactions, and user management. The information is structured in sections for easy reference and includes examples for clarity.

Uploaded by

Koletto GALY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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';

You might also like