Jump to content

Recommended Posts

Is it good to use variables from included / require files... It seems to work?

 

Is it good to use variables from while loops outside of the while loop? ...It seems to work..

 

Just 2 questions I've been pondering about and wondering if it's good practice or not even though they seem to work.

1. Make sure there are no conflicts. I can't say it's bad or good. Best if they're in classes or functions. So I guess it's bad because you can sometimes unintentionally overwrite an important variable in your included files.

 

2. Depends, but generally you want to avoid that. Declare the variable before the while loop first is best.

It depends on how it affects the readability of the code.  As far as the interpreter is concerned, the included/required file is part of the script, just like if you had cut and pasted the contents of the file into your main file, so there's no technical reason not to use variables from it.  I'd suggest not using variables from the included file if you can help it, though, since it makes the code less readable.  (Someone who is skimming your code and doesn't bother to open the included file will have no idea where the variable came from).

 

As for the while loop, there's also no technical reason not to use variables that were created in them, but it's not a good idea from a logic standpoint.  If circumstances are such that the loop never executes, you'll end up trying to use a variable that never gets defined.  I'd just define the variable with a default value before writing the loop, to be on the safe side.  Then you can use the variable however you want.

 

Hope that helps.

Everyone pretty much covered things except for question 1:

 

No on included files, yes on required files.

 

If a variable is coming from an 'include' file, if the file isn't included (which won't throw an error) then the variable will error out. If you 'require' the file, it will error if it doesn't load up the file. Using include for things like that gives you the potential of the file not being included properly and then the variable doesn't hold the value you want it to.

 

So how can I define the variables from this while loop before writing this loop?

 

$userupdateq = "SELECT * FROM user WHERE id = '$idsession'";
if ($userupdater = mysql_query($userupdateq)) {
while ($userupdaterow = mysql_fetch_array ($userupdater)) {
$name= $userupdaterow['name'];
$name2 = html_entity_decode($name);
$email = $userupdaterow['email'];
$email2 = html_entity_decode($email);
             }
}
//Incase while doesn't execute don't use variables here?

You could just give them blank default values, e.g. $name = ''; $name2 = ''; etc. right before the loop.

 

And now I'm going to make myself sound like a total noob:

 

You need them in the while loop because for each record, they will change.

 

If it's only one record your getting, youdon't need the while() loop at all.

 

Then how do you get the values?  I've always used while even when I know that there's only one row.  Can you just write $row = mysql_fetch_assoc($result); without a loop?

 

If I know there will be one record, I always include LIMIT 1 just to help MySQL.  *hugs MySQL*

 

Does that actually have an effect on the speed at which MySQL retrieves the result?  (using LIMIT 1, I mean, not hugging MySQL. :))  Thinking about it now, it seems like that would make sense.  It would just stop as soon as it finds the row, instead of going over all the rows.  It never occurred to me before to do that, though.

Does that actually have an effect on the speed at which MySQL retrieves the result?  (using LIMIT 1, I mean, not hugging MySQL. :))  Thinking about it now, it seems like that would make sense.  It would just stop as soon as it finds the row, instead of going over all the rows.  It never occurred to me before to do that, though.

MySQL will be friendly to you if you're friendly to it. :)

using LIMIT 1, I mean, not hugging MySQL.

 

Lol.

 

And yeah, you can just define a variable to fetch your query, you don't have to put it in a while loop. The only reason it's put in the while loop is to continue receiving all the records, not the just the first one.

 

If you only want the first one, then there's no point in putting it in the while loop.

If you only want the first one, then there's no point in putting it in the while loop.

 

Now let me ask you this, why in the world would you want to return all the records in a table when you only want one? If you are only expecting 1 record, adding the LIMIT 1 ensures this no matter what which should free up memory etc.

 

I always use this practice especially with updates/deletion's if I only want to delete 1 record or update 1 record. Even if I want to validate a user I always LIMIT 1, granted my usernames are unique, but yea. It just makes sense that if you know you only want one record to LIMIT 1 so you do not waste memory/mysql resources.

Now let me ask you this, why in the world would you want to return all the records in a table when you only want one? If you are only expecting 1 record, adding the LIMIT 1 ensures this no matter what which should free up memory etc.

 

If you know that the field in your WHERE clause is unique, then only one row will get returned regardless.  (e.g. "SELECT * FROM foo WHERE ID = $bar" when ID is the primary key).  My question was whether using LIMIT 1 has any effect on the efficiency of the query.  (like if it makes MySQL stop searching as soon as it find the row, whereas without the limit it would keep searching even though it already found the one row it's going to return.)

 

Also, the IT department gets weirded out when I try to hug their database servers...

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.