PHP Cheat Sheet

$_POST[] // form variable
$_GET[] // URL variable
include() // include
$_SESSION[] // session variable

Get PHP install info

phpinfo();


Connect to database

$host_name = “dbhost.com”;
$database = “dbname”;
$user_name = “dbusername”;
$password = “dbpassword”;

$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (mysqli_connect_errno())
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}
else echo ‘You are connected to the <strong> ‘.$database.'</strong> database<br /><br />’


 

Create Table

$sql = “CREATE TABLE JOBS (
JOBS_ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
JOBS_TITLE VARCHAR(256) NOT NULL,
JOBS_DESCRIPTION VARCHAR(256) NULL,
JOBS_LINK VARCHAR(256) NULL
)”;

if (mysqli_query($connect, $sql)) {
echo “Table JOBS created successfully”;
} else {
echo “Error creating table: ” . mysqli_error($conn);
}

mysqli_close($connect);


Join Tables

$open_jobs = “SELECT
JOBS.JOBS_ID,
JOBS.JOBS_TITLE,
PROPERTIES.PROPERTIES_ID,
PROPERTIES.PROPERTIES_TITLE,
PROPERTIES.PROPERTIES_LINK,
JOBS.JOBS_LINK,
JOBS_PROPERTIES.JOBS_PROPERTIES_ID,
JOBS_PROPERTIES.JOBS_PROPERTIES_STATUS
FROM JOBS
INNER JOIN JOBS_PROPERTIES ON JOBS.JOBS_ID = JOBS_PROPERTIES.JOBS_ID
INNER JOIN PROPERTIES ON PROPERTIES.PROPERTIES_ID = JOBS_PROPERTIES.PROPERTIES_ID”;
$open_jobs_result = $connect->query($open_jobs);&lt