pneudralics Posted May 4, 2009 Share Posted May 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-825878 Share on other sites More sharing options...
AmandaF Posted May 4, 2009 Share Posted May 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-825886 Share on other sites More sharing options...
Zhadus Posted May 4, 2009 Share Posted May 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-825897 Share on other sites More sharing options...
pneudralics Posted May 4, 2009 Author Share Posted May 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-825898 Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-825902 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 If I know there will be one record, I always include LIMIT 1 just to help MySQL. *hugs MySQL* Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-825904 Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 Lol Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-825911 Share on other sites More sharing options...
AmandaF Posted May 6, 2009 Share Posted May 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827870 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827881 Share on other sites More sharing options...
jackpf Posted May 6, 2009 Share Posted May 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827897 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Do I sense jealousy here? *Ken2k7 gives another hug to MySQL Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827899 Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827902 Share on other sites More sharing options...
jackpf Posted May 6, 2009 Share Posted May 6, 2009 Ok, I was just explaining why not to put queries into a while loop when you're only getting one record... I wasn't suggesting not to append LIMIT 1 to the query, rather suggesting not to put it in a while loop. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827909 Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 I wasn't suggesting not to append LIMIT 1 to the query, rather suggesting not to put it in a while loop. Then we are in agreeableness. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827914 Share on other sites More sharing options...
jackpf Posted May 6, 2009 Share Posted May 6, 2009 Indubitably. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827917 Share on other sites More sharing options...
AmandaF Posted May 6, 2009 Share Posted May 6, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827955 Share on other sites More sharing options...
jackpf Posted May 6, 2009 Share Posted May 6, 2009 It appears that LIMIT does optimise performance, from what I understand. Here's a good read - http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827965 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 AmandaF - they're just being cautious because if you accidentally knocked them down, they'll be in trouble. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-827970 Share on other sites More sharing options...
AmandaF Posted May 7, 2009 Share Posted May 7, 2009 Hmm, good to know about the LIMIT. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/156809-a-few-questions-about-variables-that-ive-been-wondering/#findComment-828439 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.