Page 1 of 1

Code Glitch : If its the same thing why doesnt it work

Posted: Thu Oct 25, 2012 7:49 am
by Da_Elf
Ive got a database with a bunch of translations and my site initially checks if the user has set a language, if not then it checks the browser language and uses that.The translations are stuck into an array. This language code is used around the site and i had everything working nicely. On a few of the pages i decided to tidy us the code a bit to cut down on how many lines there were and then all came to a grinding halt. I dont know why since in theory its the exact same code....
Original code

Code: Select all

if (strlen(mysql_real_escape_string($_GET['lang'])) == 2){$lc = mysql_real_escape_string($_GET['lang']);};
$lar = array('empty');
$langselect = "SELECT * FROM lang ORDER BY lid";
$langf = mysql_query($langselect, $dbConn);
while ($lf = mysql_fetch_array($langf, MYSQL_ASSOC)) {
	$lar[] = $lf[$lc];
}
Second Code that freezes the site

Code: Select all

if (strlen(mysql_real_escape_string($_GET['lang'])) == 2){$lc = mysql_real_escape_string($_GET['lang']);};
$lar = array('empty');while ($lf = mysql_fetch_array(mysql_query("SELECT * FROM lang ORDER BY lid", $dbConn), MYSQL_ASSOC)) {$lar[] = $lf[$lc];}

Re: Code Glitch : If its the same thing why doesnt it work

Posted: Thu Oct 25, 2012 8:14 am
by twinedev
The problem is that each iteration through the while loop, you are doing a fresh query to the database, so it goes into an infinite loop. You need to get the mysql_query() out of the while statement.

-Greg