Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$distinct = "DISTINCT"; // or empty
$columns = "a, b, c"; // columns
$table = "thetable"; // table to retrieve content from
$delete = false; // Fetch records marked deleted
$conditions = ""; // Series of conditions
$orderCol = "a"; // Order by
$orderDir = "ASC"; // Order by ASC/DESC
$limit = ""; // Optional limit number
$sql = sprintf(
"
SELECT %s %s
FROM %s
WHERE deleted = %d AND %s
ORDER BY %s %s %s",
$distinct,
$columns,
$table,
$delete,
$conditions,
$orderCol,
$orderDir,
$limit ? sprintf("LIMIT %d", $limit) : ""
);
// vs
$sql = "
SELECT $distinct $columns
FROM $table
WHERE deleted = " . (int) $delete . " AND $conditions
ORDER BY $orderCol $orderDir " . (
$limit ? "LIMIT " . (int) $limit : ""
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment