PDA

View Full Version : PHP Overload



Voetsjoeba
July 5th, 2003, 12:32 PM
Hi everyone :)

I am trying to get data from a MySQL database into Flash. In flash, I have

loadVariables("news.php","content");


on the first frame of the main movie where content is the movieclip the variables should be loaded into. Inside content, I have a dynamic text field with the var 'inhoud'. On the left of that dynamic textfield, there is an empty movieclip called 'img', in which an image should load. So on that movieclip I have


onClipEvent(load){
_parent.img.loadMovie("home"+i+".jpg");
}


Now, the php file "news.php" contains the following:


<?
mysql_connect("localhost", "voetsjoeba","--hidden--");
mysql_select_db("voetsjoeba");

$qr = mysql_query("SELECT * FROM news");

$nrows = mysql_num_rows($qr);
for ($i=0; i < $nrows; $i++){
$row = mysql_fetch_array($qr);

$output="";
$output.= "&i=".$nrows;
$output.= "&img".$i."=".$row['img'];
$output.= "&datum".$i."=".$row['datum']."<br><br>";
$output.= "&inhoud".$i."=".$row['inhoud'];

print $output;
}
?>

Now when I tested the movie, it didn't work. So I viewed the php file, and this HUGE list comes up, which just grows longer and longer. It almost overloads my computer. What do I have to do to prevent this ?

ahmed
July 5th, 2003, 01:03 PM
you're causing an infinite loop i assume.. try this:


$row = mysql_fetch_array($qr);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$output="";
$output.= "&i=".$nrows;
$output.= "&img".$i."=".$row['img'];
$output.= "&datum".$i."=".$row['datum']."<br><br>";
$output.= "&inhoud".$i."=".$row['inhoud'];
print $output;
}

Voetsjoeba
July 5th, 2003, 01:14 PM
Nope :-\

ahmed
July 5th, 2003, 01:18 PM
oh.. i think the query's wrong..
$qr = mysql_query("SELECT * FROM news WHERE <criteria>");

Voetsjoeba
July 5th, 2003, 01:32 PM
Still nothing :-\ Thanks for your help though :) Am I supposed to change <criteria> with something ? If so, what ?

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/voetsjoeba/HTML/test/news.php on line 7

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/voetsjoeba/HTML/test/news.php on line 8

ahmed
July 5th, 2003, 01:33 PM
lol the line i gave doesnt work, you need to put your own criteria in there :)

Voetsjoeba
July 5th, 2003, 01:33 PM
Yeah, but what !? I have four columns, called, newsID, img, datum and title.

ahmed
July 5th, 2003, 01:42 PM
what exactly are you trying to extract out of the db??

Jubba
July 5th, 2003, 01:43 PM
put the


print output;


outside of your for loop not inside it.

Jubba
July 5th, 2003, 01:46 PM
code should look like this



<?
mysql_connect("localhost", "voetsjoeba","--hidden--");
mysql_select_db("voetsjoeba");

$qr = mysql_query("SELECT * FROM news");

$nrows = mysql_num_rows($qr);
for ($i=0; i < $nrows; $i++){
$row = mysql_fetch_array($qr);

$output="";
$output.= "&i=".$nrows;
$output.= "&img".$i."=".$row['img'];
$output.= "&datum".$i."=".$row['datum']."<br><br>";
$output.= "&inhoud".$i."=".$row['inhoud'];
}

print $output;
?>


because you are telling the loop to print out the string output every time the loop executes and the loop gets bigger and bigger with each loop. thats why it prints out so much info.

Voetsjoeba
July 5th, 2003, 01:46 PM
Well basically ... only the text :P That would be 'datum' and 'inhoud'. The image that should be loaded is "home"+i+".jpg", where i = $nrows. So that basically means that if I create another row (with other content), it goes to home2.jpg, and so on.

Now that I think about it, the '<br><br>' I have there is totally useless, as the PHP is only defining vars for Flash, and not filling in the vars in Flash ... and the 'img' column is useless too ...

ahmed
July 5th, 2003, 01:47 PM
hm.. i usually echo the outputed data IN the loop, but no such thing happens :)


[edit]

ok i see, since he doesnt have a criteria to be met, he'll need to have the print function outside the loop.. am i getting it right? lol

Voetsjoeba
July 5th, 2003, 01:48 PM
Didn't notice your post Jubba :) Going to try it now ...

Jubba
July 5th, 2003, 01:49 PM
not necessarily. I think its just because he is storing all the data inside that string and then telling the loop to print out that string with each loop. You don't realyl want that. You want to add all the data to the loop and then once the loop is finished print out the data.

Voetsjoeba
July 5th, 2003, 01:51 PM
No it still doesn't work. The PHP file now generates the whole loop, and is then supposed to write that whole bunch, but I didn't wait for that :P. Can I fix the loop somehow ?

Jubba
July 5th, 2003, 01:51 PM
can you give us a link to the PHP file? so we can see what its printing out?

Jubba
July 5th, 2003, 01:52 PM
which code are you using?

Voetsjoeba
July 5th, 2003, 01:53 PM
Originally posted by Jubba
not necessarily. I think its just because he is storing all the data inside that string and then telling the loop to print out that string with each loop. You don't realyl want that. You want to add all the data to the loop and then once the loop is finished print out the data.

And how would I do that :-\ ?

Jubba
July 5th, 2003, 01:54 PM
you have to wait for the loop to finish executing and then print out the data as one long string so Flash can read it. Use the code that I gave you in one of my previous posts.

Voetsjoeba
July 5th, 2003, 01:55 PM
Well, right now I'm using:



<?
mysql_connect("localhost", "voetsjoeba","--hidden--");
mysql_select_db("voetsjoeba");

$qr = mysql_query("SELECT * FROM news");

$nrows = mysql_num_rows($qr);
for ($i=0; i < $nrows; $i++){
$row = mysql_fetch_array($qr);

$output="";
$output.= "&i=".$nrows;
$output.= "&img".$i."=".$row['img'];
$output.= "&datum".$i."=".$row['datum'];
$output.= "&inhoud".$i."=".$row['inhoud'];
}

print $output;
?>


and the link is http://voetsjoeba.xszone.nl/test/news.php. Right now it does nothing, cuz it's still in that loop, and keeps generating data and thus doesn't print anything (I guess, cuz I ain't waiting for it )

Jubba
July 5th, 2003, 01:58 PM
<?
$l = mysql_connect("localhost", "voetsjoeba","--hidden--");
mysql_select_db("voetsjoeba");

$q = "SELECT * FROM news";
$r = mysql_query($q, $l);

$nrows = mysql_num_rows($r);

print $nrows;

mysql_close($l);

?>

Voetsjoeba
July 5th, 2003, 01:58 PM
Well, I've waited for it to finish now, and it sais this:

Fatal error: Maximum execution time of 30 seconds exceeded in /home/voetsjoeba/HTML/test/news.php on line 8

:-\

Jubba
July 5th, 2003, 01:58 PM
try that and tell me what number it prints out.

Voetsjoeba
July 5th, 2003, 01:59 PM
Originally posted by Jubba


<?
$l = mysql_connect("localhost", "voetsjoeba","--hidden--");
mysql_select_db("voetsjoeba");

$q = "SELECT * FROM news";
$r = mysql_query($q, $l);

$nrows = mysql_num_rows($r);

print $nrows;

mysql_close($l);

?>


K, lemme try that :)

Voetsjoeba
July 5th, 2003, 02:01 PM
1.

Jubba
July 5th, 2003, 02:02 PM
is there only one row in your news table?

Voetsjoeba
July 5th, 2003, 02:03 PM
Yeah, for now only one, I'm going to add some later.

Jubba
July 5th, 2003, 02:06 PM
<?
$l = mysql_connect("localhost", "voetsjoeba","--hidden--");
mysql_select_db("voetsjoeba");

$q = "SELECT * FROM news";
$r = mysql_query($q, $l);

if($r){
$nrows = mysql_num_rows($r);

$output="";

for ($i=0; i < $nrows; $i++){
$row = mysql_fetch_array($qr);

$output .= "&i=".$i;
$output .= "&img".$i."=".$row['img'];
$output .= "&datum".$i."=".$row['datum'];
$output .= "&inhoud".$i."=".$row['inhoud'];
}
print $output;
}
else
{
print mysql_error($l);
}



mysql_close($l);
?>


see what that gives you.

Voetsjoeba
July 5th, 2003, 02:09 PM
a million times Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/voetsjoeba/HTML/test/news.php on line 14

Jubba
July 5th, 2003, 02:10 PM
lol oops. yeah

change that line to



$row = mysql_fetch_array($r);


forgot to change the result variable...

Jubba
July 5th, 2003, 02:11 PM
OMG! I just saw the error that was causing all these problems! look at your for loop! The middle statement! You aren't using a variable! You are just saying i it has to be $i!!!!

Voetsjoeba
July 5th, 2003, 02:12 PM
It looks like it's starting it's loop again ... *waits ...* Here's what is sais:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 9 bytes) in /home/voetsjoeba/HTML/test/news.php on line 16

Jubba
July 5th, 2003, 02:13 PM
look at my last post ^

Voetsjoeba
July 5th, 2003, 02:14 PM
Originally posted by Jubba
OMG! I just saw the error that was causing all these problems! look at your for loop! The middle statement! You aren't using a variable! You are just saying i it has to be $i!!!!

AAAAAAAAAAAAAAAAAAAARGH :P Thanks for noticing that ! I'm gonna try my original code with that changed.

Jubba
July 5th, 2003, 02:14 PM
stupid small insignificant errors!

Voetsjoeba
July 5th, 2003, 02:14 PM
Yeah I know we're posting all through eachother, you're always a little bit before me :P

Voetsjoeba
July 5th, 2003, 02:19 PM
IT WORKS !!! http://voetsjoeba.xszone.nl/test/news.php.

Jubba: thankyouthankyouthankyouthankyou....x1000....

:P

I'm so happy ! Thanks Jubba (and ahmed ;) )