datanut Posted July 1, 2011 Share Posted July 1, 2011 I'm just getting started with PHP and MySQL, and I've noticed a difference in naming the query variable. Is there a difference in using $sql vs $query? For example this code $sql = "SELECT COUNT(*) FROM tblname"; $result = mysqli_query($dbc, $sql); brings the same as: $query = "SELECT COUNT(*) FROM tblname"; $data = mysqli_query($dbc, $query); Is it a matter of semantics? The book I've been reading uses "$query," but the tutorials on phpfreaks use "$sql." Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/ Share on other sites More sharing options...
mikosiko Posted July 1, 2011 Share Posted July 1, 2011 I use $doesnotmatteratall ... is just a variable name... Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1237338 Share on other sites More sharing options...
fenway Posted July 2, 2011 Share Posted July 2, 2011 Everything matters -- either it's the SQL statement (a string) or the query handle (an object). Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1237727 Share on other sites More sharing options...
DavidAM Posted July 2, 2011 Share Posted July 2, 2011 From the standpoint of the compiler (or, in this case, the interpreter) it does not matter what name you use for the variable. You could call them $apple and $orange for all it cares. From a programmer standpoint, it is helpful to use variable names that help you remember what it is. I generally use $sql for the query string and $res for the query resource (mysql_query()). Then I use $row for the returned data (mysql_fetch()). When I see "if(empty($res))" somewhere in my code, I know it is checking the query resource, not the query string and not the returned data. The important thing is to choose a naming convention that works for you, and be consistent. When you are working on a team, these naming conventions are (or should be) defined and documented, so everyone on the team can quickly identify a variable no matter who wrote (or last worked on) the code. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1237744 Share on other sites More sharing options...
fenway Posted July 2, 2011 Share Posted July 2, 2011 Yes, but we're talking about variable NAMES, not variable IDENTIFIERS -- so it's only beneficial to the human in question. And it matters even if you're not working on a team -- going back into 5-year-old code and trying to guess what a variable holds is all but impossible. A simple convention will save you hours. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1237746 Share on other sites More sharing options...
ebmigue Posted July 6, 2011 Share Posted July 6, 2011 And going on 20 years from now, SQL might not be anymore in existence (or its status might be that of COBOL). So use $query instead. IMO, people will still be "querying" databases 20 years from now, but not necessarily using SQL (some other db language might be in use). $query is more general. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1238833 Share on other sites More sharing options...
fenway Posted July 6, 2011 Share Posted July 6, 2011 If SQL isn't in existence, then the $sql variable won't be useful for the query -- which is exactly what it's an ideal choice. You should name variables based on what they contain, not based on what they're used for. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1238959 Share on other sites More sharing options...
ebmigue Posted July 7, 2011 Share Posted July 7, 2011 If SQL isn't in existence, then the $sql variable won't be useful for the query -- which is exactly what it's an ideal choice. Huh? If you use $query, your variables names will not become obsolete. If you use $sql, and $sql is no longer used by the public, your code becomes obsolete. At least in naming conventions. You should name variables based on what they contain, not based on what they're used for. Wow. Should I declare a variable $one, because it will contain the value 1? What if it can contain the value 2? It is untenable. I say, name things according to their purpose. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1239403 Share on other sites More sharing options...
fenway Posted July 7, 2011 Share Posted July 7, 2011 Don't name based on "value" -- named based on "contents". In your example, what does "1" represent? Purpose is subjective, and hence subject to change. You don't want to ever have to change the variable names - EVER. And yes, if SQL goes away, then I would really hope that you weren't expecting to use code that required it -- but that doesn't make the variable that contains the SQL any less valid. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1239524 Share on other sites More sharing options...
ebmigue Posted July 7, 2011 Share Posted July 7, 2011 Don't name based on "value" -- named based on "contents". In your example, what does "1" represent? But is not the value of a variable the variable's contents? Thus, both things are the same. In my example, 1 represents the integer 1. Purpose is subjective, and hence subject to change. You don't want to ever have to change the variable names - EVER. I'd rather not comment. It is difficult to arrive at an intelligent conclusion, when preference is involved. I would just like to note that, if your suggestion is to be taken seriously, then the PHP community is in horrible error and malpractice. See their parameter names for the built-in PHP functions (e..g, the string functions)? Yep, the parameter names (variable names) are based on the parameters purpose in the function. In that case, they are in error. And yes, if SQL goes away, then I would really hope that you weren't expecting to use code that required it -- but that doesn't make the variable that contains the SQL any less valid. I do not understand any of this. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1239537 Share on other sites More sharing options...
fenway Posted July 7, 2011 Share Posted July 7, 2011 Actually, the 7 different versions of regex string functions in PHP is, IMHO, a "horrible error". The value is not the same as the contents -- 1 is meaningless -- one what? Again, this is my personal preference from 20+ years of programming experience -- people can do whatever they want. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1239558 Share on other sites More sharing options...
ebmigue Posted July 7, 2011 Share Posted July 7, 2011 Actually, the 7 different versions of regex string functions in PHP is, IMHO, a "horrible error". Ok. The value is not the same as the contents I disagree. A variable's current value == A variable's current contents. -- 1 is meaningless -- one what? The integer 1. Are you saying that the integer 1 has no meaning? Wow. Mathematics is in trouble. Again, this is my personal preference from 20+ years of programming experience -- people can do whatever they want. Which begs the question: why have this discussion, when in the end, "people can do what they want", given "that 20+ years of programming experience" is involved. I rest my case. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1239565 Share on other sites More sharing options...
fenway Posted July 7, 2011 Share Posted July 7, 2011 If you'd like to create a constant to store "1", then name it $ONE. Otherwise, name it to describe "1 what"? Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1239822 Share on other sites More sharing options...
ebmigue Posted July 9, 2011 Share Posted July 9, 2011 If you'd like to create a constant to store "1", then name it $ONE. Otherwise, name it to describe "1 what"? I really hope the PHP community will heed your advice. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1240379 Share on other sites More sharing options...
fenway Posted July 10, 2011 Share Posted July 10, 2011 Let's agree to disagree. Quote Link to comment https://forums.phpfreaks.com/topic/240881-sql-vs-query/#findComment-1240840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.