Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Cheat Sheet

PHP & MySQL For Dummies

When writing PHP programs to create your Web database application and communicate with a MySQL database, you have many things to remember. You must spell code correctly and use perfect syntax. This Cheat Sheet provides a reference for quickly looking up the correct syntax for the code you use most frequently.

Use PHP Statements to Create Programs

PHP programs are a series of PHP statements, executed one at a time. Here’s a list of the PHP statements (with syntax) that you use when writing PHP programs:

array ( "key" => "value", … );
die("message");
do { block } while (condition);
echo item;
extract($array);
for (startingval; endingval;incremnt) { block }
foreach( $array as $key => $value) { block }
function funcname(value,value,…) { block }
header("Location: URL");
if (condition) { block }
   elseif (condition) { block }
   else { block }
number_format(number,decimals);
session_start();
session_destroy();
switch var { case value statements break; … }
unset();
while (condition) { block }

Communicate with a Database through PHP MySQL Functions

PHP communicates with MySQL databases by using a set of functions developed specifically for this purpose. This list shows the syntax of these frequently used functions.

mysqli_connect("host","accnt","passwd")
mysqli_select_db($cxn, "dbname",)
mysqli_query($cxn,"query")
mysqli_fetch_assoc($result)
mysqli_num_rows($result)
mysqli_insert_id($cxn)

Make MySQL Database Changes with the ALTER Query

The ALTER query is used to change the structure of a MySQL database. This list shows the syntax for the changes you are most likely to want to make:

ADD colname definition
ALTER colname SET DEFAULT value
ALTER colname DROP DEFAULT
CHANGE colname newcolname definition
DROP colname
MODIFY colname definition
RENAME newtablename

Access and Adapt a Database with MySQL Queries

PHP communicates with MySQL databases by sending SQL queries. Here’s a list of SQL queries, with their syntax, that you can use to access, view, and alter the database:

ALTER TABLE table change
CREATE DATABASE database
CREATE TABLE (col def,…,PRIMARY KEY(col))
DELETE FROM tablename WHERE clause
DROP database|table
INSERT INTO table (col,col,…) VALUES (col,col,…)
LOAD DATA INFILE ″filename″ INTO TABLE table
SELECT col,col,… FROM table WHERE clause
SELECT statement UNION SELECT statement
SHOW DATABASES|TABLES
SHOW COLUMNS FROM table
UPDATE table SET col=value,… WHERE clause

Knowing the MySQL WHERE Clause Format

The WHERE clause is used to modify a DELETE, SELECT, or UPDATE SQL query. This list shows the format you can use when writing a WHERE clause:

WHERE exp AND|OR exp AND|OR exp

where exp can be one of the following:

column = value
column > value
column >= value
column < value
column <= value
column BETWEEN value1 AND value2
column IN (value1,value2,…)
column NOT IN (value1,value2,…)
column LIKE value
column NOT LIKE value

Special Characters to Use in PHP Expression Test Patterns

Regular PHP expressions use patterns to test whether the input users submit when using online forms are in the correct format. This table shows characters you can use in 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
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!