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

From: Date: Fri, 26 Jan 2007 18:18:05 +0000
Subject: Re: SQL Readability.. (was Re: most powerful php editor)
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-247844@lists.php.net to get a copy of this message
----- Original Message ----- From: "Stut" <stuttle@gmail.com>
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;
}
I use to build SQL statements with a BuildSql function, which you can see at: http://www.satyam.com.ar/int/BuildSql.php It is commented in PhpDoc format. For example: echo BuildSql('Insert into ?ptable (?s,?ns,?mi,?d,?ni,?i,?t)','Something','',5,time(),0,null,mktime(3,4,5)- mktime(0,0,0)); Will return: Insert into wp_table ('Something',null,5,'2007-01-21 15:54:27',null,0,'0 04:04:05') It is not only meant to build inserts but it is more like a sort of SQL-oriented sprintf(), like it does proper handling of null values, such as avoiding puting the text 'null' (notice the quotes) instead of the value null. It also has a ?p 'prefix' modifier to use a fixed prefix on all table names. As for formatting, I usually put the SQL statement in one line and the arguments in the next one with spaces to align them vertically, which I won't show here since the formatting of the message will ruin it anyway.

« previous php.general (#247844) next »