public static BuildSQLSelect ( string $tableName, array $whereArray = null, array/string $columns = null, array/string $sortColumns = null, boolean $sortAscending = true, integer/string $limit = null ) : string | ||
$tableName | string | The name of the table |
$whereArray | array | (Optional) An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect) |
$columns | array/string | |
$sortColumns | array/string | |
$sortAscending | boolean | (Optional) TRUE for ascending; FALSE for descending This only works if $sortColumns are specified |
$limit | integer/string | |
return | string | Returns a SQL SELECT statement |
public static function BuildSQLSelect($tableName, $whereArray = null, $columns = null, $sortColumns = null, $sortAscending = true, $limit = null)
{
if (!is_null($columns)) {
$sql = self::BuildSQLColumns($columns);
} else {
$sql = "*";
}
$sql = "SELECT " . $sql . " FROM `" . $tableName . "`";
if (is_array($whereArray)) {
$sql .= self::BuildSQLWhereClause($whereArray);
}
if (!is_null($sortColumns)) {
$sql .= " ORDER BY " . self::BuildSQLColumns($sortColumns, true, false) . " " . ($sortAscending ? "ASC" : "DESC");
}
if (!is_null($limit)) {
$sql .= " LIMIT " . $limit;
}
return $sql;
}
echo MySQL::BuildSQLDelete("Test", $values) . "\n<br />\n"; echo MySQL::BuildSQLInsert("Test", $values) . "\n<br />\n"; echo MySQL::BuildSQLSelect("Test", $values) . "\n<br />\n"; echo MySQL::BuildSQLUpdate("Test", $values, $values) . "\n<br />\n"; echo MySQL::BuildSQLWhereClause($values) . "\n<br />\n"; echo "</pre>" . "\n"; // Or create more advanced SQL SELECT statements $columns = array("Name", "Age"); $sort = "Name"; $limit = 10; echo MySQL::BuildSQLSelect("Test", $values, $columns, $sort, true, $limit); echo "\n<br />\n"; $columns = array("Color Name" => "Name", "Total Age" => "Age"); $sort = array("Age", "Name"); $limit = "10, 20"; echo MySQL::BuildSQLSelect("Test", $values, $columns, $sort, false, $limit); echo "\n<br />\n"; // The following methods take the same parameters and automatically execute! // $db->DeleteRows("Test", $values); // $db->InsertRow("Test", $values); // $db->SelectRows("Test", $values, $columns, $sort, true, $limit); // $db->UpdateRows("Test", $values1, $values2); // You can also select an entire table // $db->SelectTable("Test"); // Or truncate and clear out an entire table // $db->TruncateTable("Test"); // -------------------------------------------------------------------------- // Now you can throw exceptions and use try/catch blocks $db->ThrowExceptions = true; try { // This next line will always cause an error