Web Hosting Forums

Results 1 to 3 of 3

This is a discussion on Concatenate or Evaluate? in the Hosting Talk & Chit-chat forum
I could do time tests, but I guess I'm lazy. Which is a better way to construct a SQL statement using PHP: to concatenate the ...

  1. #1
    crazy davey flipdoubt's Avatar
    Join Date
    Aug 2002
    Location
    Plymouth, MI
    Posts
    546

    Concatenate or Evaluate?

    I could do time tests, but I guess I'm lazy.

    Which is a better way to construct a SQL statement using PHP: to concatenate the bits and pieces of the SQL statement or to evaluate the entire string?

    The query shown below is an over-simplification. Just imagine all sorts of "ifs" appending various ' AND ... ' conditions into the WHERE clause.
    PHP Code:
    // Concatenate:
    $str_sQuote chr39 ); 
    $query1  'SELECT * FROM some_table WHERE id = '.$id.
               
    ' AND some_string = '.$str_sQuote.
               
    $some_string.$str_sQuote;
    $result1 mysql_query$query1$conn );

    // Evaluate:
    $query2  "SELECT * FROM some_table WHERE id = $id AND some_string = '$some_string'";
    $result2 mysql_query$query2$conn ); 

  2. #2
    Loyal Client
    Join Date
    Jul 2001
    Posts
    579
    It depends on your personally priorities really. I highly doubt there would be any difference in speed. I would do whatever is the most readable. When you look back at your code six months from now, which one will you be able to understand the best?

  3. #3
    Loyal Client
    Join Date
    Jul 2001
    Posts
    200
    Looks to me like the only difference would come if you're running this in a loop several thousand times or so. Once or twice or even a couple dozen times shouldn't have a noticeable difference.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •