|
Posted by Darko on 11/08/07 18:55
Steve wrote:
> > - snip -
> >
> > I must agree with you on this one. I do format my queries in code, but
> > it looks something like this:
> >
> > $query = sprintf(
> > "INSERT INTO " .
> > "main " .
> > "(name, img, descr, from, size, format, cat, host, link, date)
> > " .
> > "VALUES " .
> > "('%s', '%s', '%s', '%s', %f, '%s', '%s', '%s', '%s', '%s')",
> > mysql_real_escape_string( $name ),
> > ...
> > );
> >
> > $queryResult = @mysql_query( $query );
> > if ( $queryResult === FALSE ) {
> > throw new Exception( "..." );
> > ...
> >
> > ...with exception, of course, that I would try to avoid the "from"
> > sort of variable
> > names. This is pretty shorter, and still good enough to understand (at
> > least for me)
>
> right. it is short. but, why apply formatting in one instance and not
> another? the inconsistency kills me. further, should your now simple query
> become not so simple in the future, you'll have to re-write. and second, if
> your columns name are numerous and long, how would you visually be able to
> say this value is being inserted into this column?
>
> ot for a second... why not:
>
> function prepare(&$value)
> {
> return $value = "'" . mysql_real_escape_string($value) . "'";
> }
>
> $values = array(
> $value1 ,
> $value2 ,
> ...
> );
> array_walk('prepare', $values);
> $sql = "
> insert into sample
> (
> column1 ,
> column2 ,
> )
> values
> (
> " . implode(", \r\n", $values) . "
> )
> ";
> echo '<pre>' . print_r($sql, true) . '</pre>';
>
> if you utilize array_walk, you've got only one spot to maintain the
> preparation of your values. further, if you make the function NOT utilze
> mysql_* functions but still provide the same functionality, you can move
> from db to db without ever having to do a re-write.
Of course, I utilize a DBFactory that produces an implementation of
DBConnection interface (which is either PGConnection (for Postgres) or
MySQLConnection (for MySQL). That way they have a common interface,
addSlashes() being amongst them.
> further, notice that
> even when using implode(), i can still get a nicely formatted query to the
> browser should i need to debug it...AND (sorry, *and*)
Don't worry :)
> i can easily,
> visually tell what value goes with what column. just a thought.
Yes, of course, you tend to simplify debugging queries to the point
where majority of us don't.
This is probably because, me personally, I don't like multi hundred or
multi thousand lines
long files because I get confused in them. And I still get quite along
with queries formatted
the way described above.
>
> ok, back on topic. can you tell me what the aim of this query is?
>
No, I can't :)
Cheers
[Back to original message]
|