Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

does variable needed in querying from mysql?


  • Please log in to reply
2 replies to this topic

#1 robertpeter07


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Learning:C#, PHP, JavaScript, Others

Posted 16 October 2012 - 04:33 PM

hi guys! I want to ask why do you have to use a variable to query from mysql?
what makes this difference from the above example?
$query = "SELECT * FROM Person";

querying using mysql_query function:
mysql_query("SELECT * FROM Person");

could someone explain me the difference and why use the first sample instead of second sample or vice versa?

  • 0

#2 Vaielab


    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 16 October 2012 - 04:53 PM

Both do exactly the same thing.

If you have a large script, and your code is not clean, in the first example, you may overwrite the variable $query.

But having your sql code separated is easier to read.

So both are good.

Some fan of micro-optimization will tell you that the second way is faster since you don't have to store the string inside a variable... but that is not what will really slow your code.
  • 0

You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !

#3 Orjan


    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 17 October 2012 - 12:17 AM

Using the first method eases debugging, as you can print the $query variable so you can see if your query is right even after eventually inserted variables in the query. Otherwise, no difference. more of a personal preference.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download