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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



----- Original Message ----- From: "Stut" <stuttle@xxxxxxxxx>
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.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux