Cheat Sheet

PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies Cheat Sheet

From PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies

By Steve Suehring, Janet Valade

Create web documents and websites by knowing basic HTML elements, PHP statements and functions, and more. Become familiar with these special programming languages, which you can use to build dynamic websites that work with the MySQL database.

Basic HTML Elements

HyperText Markup Language (HTML) is the language of the web, where elements dictate the formatting and style of your content. HTML elements compose the downloaded coding that you see when you go to a web page in your web browser (such as Internet Explorer, Firefox, or Safari). Here are some basic elements to get you started building a web page.

Element Description
<a> An anchor is typically used to create links.
<body> Creates the body element for a page.
<br> Creates a line break.
<div> Creates a block to contain other HTML and text.
<doctype> Declares a document type for the HTML page.
<html> Creates the HTML element for a page.
<img> Creates a place for an image.
<span> Creates a space for an inline element within a page.

Common MySQL Queries

To make a request that MySQL can understand, you build a SQL statement and send it to the MySQL server. The bold words in the table are the MySQL query names:

Statement Description
Makes changes to the table schema.
CREATE DATABASE database Creates a database.
Creates a table.
DELETE FROM tablename
WHERE clause
Deletes data from a table.
DROP database|table Deletes a database or table.
(col,col,. .
) VALUES (col,col,. .
Adds data to a table.
filename″ INTO TABLE table
Adds data from a file to a table.
SELECT col,col,. . .FROM table WHERE
Queries a table.
SHOW DATABASES|TABLES Displays information about a database or table.
SHOW COLUMNS FROM table Displays information about a table’s column.
UPDATE table SET col=value,. ..WHERE clause Changes data within a table.

PHP MySQL Functions

The PHP language provides functions that make communicating with MySQL extremely simple. You use PHP functions to send SQL queries to the database. You don’t need to know the details of communicating with MySQL; PHP handles the details. You only need to know the SQL queries and how to use the PHP functions. Here are some common functions related to PHP and MySQL.

Function Description
mysqli_connect(host,accnt,passwd,dbname) Connects to a MySQL database.
mysqli_query($cxn,query) Sends a query to a MySQL database.
mysqli_fetch_assoc($result) Retrieves an array of data from a query.
mysqli_num_rows($result) Returns the number of rows from the previous query.
mysqli_insert_id($cxn) Retrieves the last inserted ID for an INSERT statement.

PHP Statements

PHP is a scripting language designed specifically for use on the web. It has features to aid you in programming the tasks needed to develop dynamic web applications. This table provides a brief overview of some of the statements you’ll use frequently:

Statement Description
array ( key => value, . . . ); Casts a key and value as an array.
die(message); Stops processing and displays the message.
do { block
} while (condition);
Performs an operation once and then while a condition is still
echo item; Displays item.
for (startingval;
endingval;increment) { block
Begins a loop while startingval is less than endingval.
$array as $key => $value) { block }
Begins a loop through an array.
function funcname(value,value,. .
) { block }
Declares a function.
header(Location: URL); Sends an HTTP header.
if (condition) { block } Begins a conditional block.
elseif (condition) { block } Provides an alternate test in a conditional block.
else { block } Provides a final alternative in a conditional block.
session_start(); Starts or resumes a session.
session_destroy(); Destroys a session (but doesn’t necessarily clear it
unset(); Destroys a variable.
while (condition) { block } Creates a loop while a condition is met.

Add Special Characters to Patterns in PHP Scripts

When you’re working with PHP scripts, you sometimes need to compare character strings to see if they fit certain characteristics, rather than to see if they match exact values. For example, you might want to identify strings that begin with S or strings that have numbers in them. For this type of comparison, you compare the string to a pattern. These patterns are called regular expressions. Here’s a reference sheet that includes some of the special characters that you’ll use when creating patterns:

Character Meaning Example Match Not a Match
^ Beginning of line ^c cat my cat
$ End of line c$ tic stick
. Any single character .. me, go a
? Preceding item is optional mea?n mean, men moan
( ) Groups literal characters m(ea)n mean men,mn
[ ] Any character in set abc[1-3] abc1,abc2 abc4
[^ ] Any character not in set m[!ea]n min, mon men, man
+ One or more door[1-3]+ door111, door131 door, door55
* Zero or more door[1-3]* door, door311 door4, door445
{ , } Range of repetitions a{2,5} aa,aaaaa a, xx3
Escapes character m*n m*n men, mean
( | | ) Alternate strings (Tom|Tommy) Tom, Tommy Thomas, To