Skip to content

Instantly share code, notes, and snippets.

@patrickallaert
Created June 30, 2017 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickallaert/597dc4f5393d9577972befab4988e460 to your computer and use it in GitHub Desktop.
Save patrickallaert/597dc4f5393d9577972befab4988e460 to your computer and use it in GitHub Desktop.
<?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