ARGH, why won't this work?!?!(PHP)

Captain Code

Moderator
Staff member
Mod
I have a PHP script that's crashing on this code:

Code:
		$i = 0;
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
			printf("prices[%s] = $line['price'];", $i);
			$i++;
		}

From what I can tell it should be working..

The error I get is:
"arse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /Library/WebServer/Documents/order.php on line 22
"
Anyone have any clue as to why this is not working???
Thanks
 
I'm not a PHP expert, but it would help to know which is Line 22.
My guess is that you have an extra ';' in there which it doesn't need. This is just a guess going on the fact that if you leave a ';' off the end of a line, it will say something like 'Unexpected T_STRING on line X'.

Just a guess :)
 
Line 22 is the while... line
But, if I just have an empty while loop the problem goes away, so it seems that the error is actually the line right after while...

prices[] is an array in Javascript, $line should be one record which I get a price out of.

I want the script to output something like this in Javascript:

prices[0] = 31;
prices[1] = 45;
prices[2] = 50;
etc..

So, because it's Javascript, it needs that semi colon in there..

The error seems to be with accessing $i as it thinks it's not a real variable or something, but I'm stumped as to why that is...
 
OK, I figured it out :D

Apparently, it doesn't like the $line['price'];

I guess it only lets you print out variables inside a string, and not access a record.

Code:
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	printf("prices[%u] = ", $i);
	print $line['price'];
	print ";\n";
	$i++;
}
 
Back
Top