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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

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

Post 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];}
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post 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
Post Reply