Jump to content

$sql vs $query


datanut

Recommended Posts

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."

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.