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

From: Date: Fri, 26 Jan 2007 18:35:38 +0000
Subject: Re: SQL Readability.. (was Re: most powerful php editor)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-247846@lists.php.net to get a copy of this message
On Fri, 2007-01-26 at 12:25 -0500, tg-php@gryffyndevelopment.com wrote: > 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"; > 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. My insert style is very similar to my select style: <?php $query = "INSERT INTO someTable " ."( " ." field1, " ." field1, " ." field1 " .") " ."VALUES " ."( " ." ".$db->quote( $value1 ).", " ." ".$db->quote( $value2 ).", " ." ".$db->quote( $value3 )." " .") "; ?> Or if there's a lot of fields: <?php $data = array ( 'field1' => $value1, 'field2' => $value2, 'field3' => $value3, ... ); $query = "INSERT INTO someTable " ."( " . implode( ", ", array_keys( $data ) )." " .") " ."VALUES " ."( " . implode( ", ", $db->quoteArray( $data ) )." " .") "; ?> Although, I don't find myself doing much in the way of inserts these days since I often extend a data object class that performs the inserts and updates as necessary. Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#247846) next »