Re: SQL Readability.. (was Re: most powerful php editor)
| From: | tg-php at gryffyndevelopment dot com | Date: | Fri, 26 Jan 2007 17:25:10 +0000 |
| Subject: | Re: SQL Readability.. (was Re: most powerful php editor) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-247841@lists.php.net to get a copy of this message | ||
Strangely enough, Stut and Jochem, I DO find this more readable. Hah. I know, I'm insane. I
have done it the way you guys proposed, using an associative array and using the keys and values as
the columns and insert values. While that is what I'd call "tighter" code and when
you understand what it's doing, is just as simple to maintain as how I do it, I do find my
method more 'readable'.
I tend to build queries in WinSQL first, then insert them into my PHP code. Some of which are
fairly complicated and I find if I keep my PHP code similar to my SQL code, it makes it easier to go
back and forth to tweak it. They both have a similar look to me.
So instead of using:
$query = "SELECT BunchOfJoinedColumns";
$query .= " FROM BunchOfJoinedTables";
$query .= " WHERE SomeConditions";
$query .= " AND MoreConditions";
for long complicated SELECT statements, then using the method you guys use for INSERT statements,
I'm keeping my code consistant whether it's SELECT, INSERT, UPDATE, whatever.
To me, consistancy wins out in a choice between two (imo) equally easily maintained coding styles.
But hey.. I'm always willing to learn new stuff. One reason I posted this was to see more of
what other people did with their code, SQL queries in particular.
Cheers!
-TG
= = = Original message = = =
tg-php@gryffyndevelopment.com wrote:
> My contribution to the insanity.. INSERT statements made easy:
>
> $genericQY = "INSERT INTO MOD_LMGR_Leads ("; $genericQYvalues = " VALUES
> (";
> $genericQY .= " FirstName,"; $genericQYvalues .= "
> 'John',";
> $genericQY .= " LastName"; $genericQYvalues .= "
> 'Smith'";
> $genericQY .= " )"; $genericQYvalues .= " );";
> $genericQY .= $genericQYvalues;
> $genericRS = mysql_query($genericQY);
You call that readable??
$vals = array();
$vals['FirstName'] = 'John';
$vals['LastName'] = 'Smith';
$query = mysql_query(BuildInsert('MOD_LMGR_Leads', $vals));
function BuildInsert($table, $values)
foreach (array_keys($values) as $key)
$values[$key] = mysql_real_escape_string($values[$key]);
$sql = 'insert into
'.$table.' (`';
$sql.= implode(',', array_keys($values));
$sql.= '`) values ("';
$sql.= implode('","', array_values($values));
$sql.= '")';
return $sql;
Note that this is a *very* cut down and untested version of BuildInsert.
-Stut
___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.