Re: [PHP] SQL Readability.. (was Re: most powerful php editor)



tg-php@xxxxxxxxxxxxxxxxxxxxxx wrote:
My contribution to the insanity.. INSERT statements made easy:


can't stand long var names if they're not absolutely necessary (JMO).
although I follow TG's logic here I don't find it that readable, too many
dots, [double]quotes, etc for my taste.

$genericQY = "INSERT INTO MOD_LMGR_Leads ("; $genericQYvalues = " VALUES (";
$genericQY .= " FirstName,"; $genericQYvalues .= " 'John',";
$genericQY .= " LastName"; $genericQYvalues .= " 'Smith'";
$genericQY .= " )"; $genericQYvalues .= " );";
$genericQY .= $genericQYvalues;
$genericRS = mysql_query($genericQY);


// init
$data = array();

// get (or make up) data
$data['FirstName'] = 'John';
$data['LastName'] = 'Smith';
//$data['Prefers'] = 'bitter';
//$data['promotedby'] = 'Jack Dee';

// build it
if (!empty($data)) {
$flds = join(',', array_keys($data));
$vals = join(',', $data);
$qry = "INSERT INTO MOD_LMGR_Leads ($flds) VALUES ($vals)";
} else {
die("ya think I'm stupid enough to insert nothing?");
}

// run it
$res = mysql_query($qry);

that was kind of step one of building a generic qry builder, of which there
are many :-) (and many ways to go about it)

when writing specific/custom queries I find the example below to
be the most readable way:


....


SELECT
A.field1 AS afield1,
A.field2 AS afield2,
B.field1 AS bfield1,
B.field2 AS bfield2,
C.field1 AS cfield1,
C.field2 AS cfield2,
D.field1 AS dfield1,
D.field2 AS dfield2
FROM
tableA as A
LEFT JOIN tableB AS B ON
B.fee = A.foo
LEFT JOIN tableC AS C ON
C.fii = B.fee
LEFT JOIN tableD AS D ON
D.fuu = C.fii
WHERE
A.foo = 'someValue'
ORDER BY
afield1 ASC,
cfield2 ASC


.