A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Flash only loading last row from mysql table

  1. #1
    Running Plodding & Limping SpockBert's Avatar
    Join Date
    Jun 2002
    Location
    London
    Posts
    593

    Flash only loading last row from mysql table

    Using php, trying to load the contents of about 20 rows I have in a table of a mysql database.

    I'm trying to convert this script which echo's all the rows perfectly well into the browser window:

    <?php

    $conn = mysql_connect ("localhost", "myuser" , "mypassword");

    mysql_select_db ("tutorial" , $conn);

    $sql = "SELECT * FROM testTable";

    $result = mysql_query($sql, $conn) or die ( mysql_error() );

    while ($newArray = mysql_fetch_array ($result) ){
    //give names to fields
    $id = $newArray ['id' ];
    $testField = $newArray ['testField'];
    //echo the results onscreen
    echo "The ID is <b>$id</b> and the text field is <b>$testField</b> <br>";
    }
    ?>


    that works great and spits out a series of results like this:

    The ID is 1 and the text field is Hello Earl
    The ID is 2 and the text field is Nice Car
    The ID is 3 and the text field is Go Eagles
    ************ and so on ***********
    The ID is 20 and the text field is Get Stuffed!

    now I've tried to get the same thing working in Flash, loading all the data into a variable on the main timeline called result with the code the same except for one line which I've got as:

    while ($newArray = mysql_fetch_array ($result) ){
    //give names to fields
    $id = $newArray ['id' ];
    $testField = $newArray ['testField'];
    //echo the results onscreen
    echo "&result="."$id, $testField";}
    }


    but instead of getting all the rows, I only get the last one i.e:

    20, Get Stuffed!

    anybody got any ideas?

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    you probably should not send result every time, but rather result1, result2, ,,,

    $count = 0;
    while($newArray = mysql_fetch_array ($result) ){
    //give names to fields
    $id = $newArray ['id' ];
    $testField = $newArray ['testField'];
    //echo the results onscreen
    echo "&result$count="."$id, $testField";
    $count++;
    }

    Musicman

  3. #3
    Running Plodding & Limping SpockBert's Avatar
    Join Date
    Jun 2002
    Location
    London
    Posts
    593
    cheers Musicman

    Yeah tried that works great!

    took me a while to figure out I have to have all the different text fields on flash labelled "result0" "result1" "result2" etc

    thanks for that

    is there a way of doing it so it all the data gets collected and dumped into just one variable called say "full_result"?

    I'm at a very early stage of messing around with Flash/PHP/mysql, just trying to get an idea of what can be achieved.

    I got a book on PHP/mysql that I'm currently working through but theres no mention of flash, I had hoped that I could simply translate all their HTML examples into flash but the above example demonstrates it ain't that simple!

    could you recommend any flash/php/mysql resources?

    thanks again for your help

  4. #4
    poet and narcisist argonauta's Avatar
    Join Date
    Nov 2001
    Location
    Under the bed
    Posts
    2,080
    i think there are three ways to work with PHP and Flash:

    using loadvars, you load a bunch of variables you echo from a php script

    php->xml->flash flash loads a xml generated by php. Kinda like loadvars, as the php script generates xml with a bunch of echo this and echo that. But as xml can represent a logical structure, instead of just a bunch of variables, then you have more control over content. Exmample, you can pass a php array as an xml object, each array element is a node in the xml, so you load the xml in flash, and know all of the nodes are part of one object...kinda....i love working with this concept.

    flash remoting amfphp. Haven't really tried it that much, but it's really powerfull, i think you can pass entire arrays in flash that are received in php as arrays, all data is passed in binary format. Cool for web services.

    For me, the best is php->xml->flash. It lets you keep your applications separate but working together. For example, a flash app that loads xml, wouldn't need a backend side, you could create all the xml files manually. Or you could choose whatever backend language you want, jsp, php, etc, with the only condition that the output is an xml file. Also, outputting data in xml, would let you handle it in a lot of forms, not just flash, but html using xslt, i think that's the strength of xml and why it's used so much. Also, i don't know about flashremoting with php, although macromedia has some articles written about it using amfphp, i think they don't give any clear anounce about if they are going to keep supporting amf or not. Anyway....you decide, i think flashremoting is cool too, loadvars, that i don't like (unless it's just one or two variables you're loading)
    my blog: blog.innocuo
    Sponsored by your mom.

  5. #5
    Member
    Join Date
    Apr 2003
    Posts
    67
    is there a way of doing it so it all the data gets collected and dumped into just one variable called say "full_result"?
    I have also wondered how to do this. For instance, use all of the data in one variable from a MySQL database instead of using a conter variable and incrementing it each loop it draws data from the database.

    php->xml->flash flash loads a xml generated by php
    Where can I learn more about using PHP to generate XML? I haven't used XML with flash, but I hear it can be powerful.

    Thanks,
    DigitalMac

  6. #6
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    Where can I learn more about using PHP to generate XML?
    There is no big difference to learn between
    a) flash loading a file xy.xml with text like
    <item type="1">abc</item>
    b) flash loading a "file" xy.php that reads
    <?
    $type=1; $item = "abc";
    echo "<item type=\"$type\">$item</item>";
    ?>

    There are, of course, some php functions that help you parse any php data that flash might send as a request

    Musicman

  7. #7
    Member
    Join Date
    Apr 2003
    Posts
    67
    Thanks for the info MusicMan.

    I am having trouble formatting my variables for Flash.

    PHP Code:
    while ($row mysql_fetch_array($result){
    extract($row)
    $thedate "1.$date<br>";
    echo 
    $thedate
    How can I format the data into one date variable that flash can read?

    Example:
    &thedates=1.13
    1.18
    1.20
    1.21
    1.12

    I want to be able to draw data from other colums too in addition to thedates variable.

    Example:
    &thedates=1.13
    1.18
    1.20
    1.21
    1.12
    &theplace=Zoo
    Palace
    Hospital
    Krispy Kream
    Best Buy

    Hope some of that makes sense.

    Thanks,
    DigitalMac
    Last edited by DigitalMac; 12-29-2003 at 03:09 PM.

  8. #8
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    two common ways:
    individual variables, like
    &thedate0=13&thedate1=18&thedate2=20

    A script to generate that would look like this
    Code:
    $num = 0;
    while ($row = mysql_fetch_array($result){
     extract($row);
     print "&thedate$num=$date";
     $num++;
    }
    join with a single char instead of the <br>, e.g. a "\n" (newline) or a | (any char works that does not occur within data)
    &theplace=Zoo|Palace|Best Buy

    script:
    Code:
    $places = array();
    while ($row = mysql_fetch_array($result){
     extract($row);
     $places[] = $place;
    }
    print "&theplaces=" . implode("|", $places);
    Musicman

  9. #9
    Member
    Join Date
    Apr 2003
    Posts
    67
    Thanks for the help MusicMan, worked great.

    Where can I learn more about Flash and PHP working together? Any articles, post, books that are good?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center