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 = = =

[EMAIL PROTECTED] 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.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to