Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question Which query is much faster?

    Which query would be faster and why?

    Code:
    mysql_query("select * from mytable where id=$id");

    OR


    Code:
    mysql_query('select * from mytable where id='.$id);

    Thanx

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,307
    Thanks
    4
    Thanked 205 Times in 202 Posts
    The second but there wouldn't be enough difference for you or anyone else to notice.

    It would be slightly faster simply because PHP only has to assign the $id variable instead of parsing the entire string in the first example to determine what may need to be assigned.
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    phantom007 (07-30-2008)

  • #3
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    djm is right, the second is faster but not by a noticeable amount of time. If you're running the query inside of a loop, you might notice the difference, but at that rate you should just be using prepared statements.
    zok@zoklet:~$ whereis zok
    zok: http://zoklet.net | http://zoklet.net/otg | /derzok/at/gmail/dot/com

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    The queries are identical. (???)

    I believe your question was meant to be about the PHP code execution time, in which the second example would be negligibly faster as it's not looking for any variables to parse within double-quotes.

    ...but not much faster, as your question suggests. We're talking nanoseconds here.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    Which query is much faster?
    I'm with kbluhm. The queries are identical and the php parsing time is almost identical, so there is nothing in either method that is "much" faster than the other.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Hopping on the bandwagon with kbluhm and CFMaBiSmAd.

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you run this in a loop and try to decide which one to use?

    just curios,

    PS: agree with previous posters about speed,

    regards

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The second is (minimally) faster. The first is in a parsable string format ("") while the second is only a string (''). The first would require the parser to parse the entirety of the query for variable replacement, while the second can ignore it.
    In either case, you will not notice the speed difference. You're queries can be altered to make a more significant speed improvement by targeting which fields to query instead of a wildcard. The SQL itself will have no speed differential.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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