sniperscope Posted January 26, 2011 Share Posted January 26, 2011 Hi all. I always create my sql queries like below. $user = "user"; $pass = "123456"; $host = "localhost"; $base = "test"; $cn = mysql_connect($host, $user, $pass) or trigger_error("SQL", E_USER_ERROR); mysql_select_db($base,$cn) or trigger_error("SQL", E_USER_ERROR); $QUERY = mysql_query("SELECT id FROM someTable", [size=14pt][color=red][b]$cn[/b][/color][/size]) or die(mysql_error()); while ($list = mysql_fetch_assoc($QUERY)){ echo "Some Text over loop"; } Okay here is the question (or problem). What if i have 5000 records in my Table. Doesn't this code connect DB 5000 times? If yes, then how can i optimize it? Quote Link to comment https://forums.phpfreaks.com/topic/225695-is-this-wrong/ Share on other sites More sharing options...
doddsey_65 Posted January 26, 2011 Share Posted January 26, 2011 no since the only thing that gets looped is what is in the while loop. in this case your echo. your connection is outside of the while loop therefore doesnt get looped. Quote Link to comment https://forums.phpfreaks.com/topic/225695-is-this-wrong/#findComment-1165312 Share on other sites More sharing options...
sniperscope Posted January 26, 2011 Author Share Posted January 26, 2011 Dear doddsey_65 thanks for your response. Quote Link to comment https://forums.phpfreaks.com/topic/225695-is-this-wrong/#findComment-1165315 Share on other sites More sharing options...
PinoyProgrammer Posted January 26, 2011 Share Posted January 26, 2011 All the data are there once the select statement has been executed. You can use LIMIT if you have such a huge database and want to query for only a few. Remember to close the connection. Quote Link to comment https://forums.phpfreaks.com/topic/225695-is-this-wrong/#findComment-1165319 Share on other sites More sharing options...
jcbones Posted January 26, 2011 Share Posted January 26, 2011 PHP automatically closes the connection when the script ends. Quote Link to comment https://forums.phpfreaks.com/topic/225695-is-this-wrong/#findComment-1165322 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.