Skip to main content
Inspiring
August 15, 2010
Question

datagrid populated from mysql dbase using php

  • August 15, 2010
  • 2 replies
  • 2932 views

There are three methods: loadvars, xml and flash remoting.

I have a great tutorial from Lee Brimlow but he only passes one column into a listbox.

I want various columns passed into a datagrid.

I'm not too sure how to amend the php code

<?PHP

$link = mysql_connect("Localhost","user","password");

mysql_select_db("cambridgekidsdb");

$query = 'SELECT * FROM trivia_user';

$results = mysql_query($query);

echo "<?xml version=\"1.0\"?>\n";

echo "<triviauser>\n";

while($line = mysql_fetch_assoc($results)) {

echo "<item>" . $line["user_name"] . "</item>\n";

}

echo "</triviauser>\n";

mysql_close($link);

?>

How do I "echo" out the other columns.

The flash AS3 code is:

var theXML:XML = new XML();

theXML.ignoreWhite = true;

theXML.onLoad = function() {

var nodes = this.firstChild.childNodes;

for(i=0;i<nodes.length;i++) {

theGrid.addItem(nodes.firstChild.nodeValue,i);

}

}

theXML.load("http://www.cambridgekids.es/trivia_user.php");

I'm almost there and this will be fundamental to my website as I can create important stats on hundreds of children.

Thank you in advance.

This topic has been closed for replies.

2 replies

Inspiring
August 19, 2010

Hi guys.

I'm afraid I couldn't get things to work. The good news is I GOOGLED my butt off and learnt so much about PHP and mysql you wouldn't believe it.

In the end, I have been advised to convert the php code to output xml for many reasons. Flash has zero problems with xml and code is easy.

Especially now that the new XML class in AS3 makes things easy.

This is the code I got from Kirupa.

<?php

header("Content-type: text/xml");

$host = "localhost";

$user = "root";

$pass = "";

$database = "test";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");

mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM blog ORDER BY date DESC";

$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>\n";

$xml_output .= "<entries>\n";

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){

    $row = mysql_fetch_assoc($resultID);

    $xml_output .= "\t<entry>\n";

    $xml_output .= "\t\t<date>" . $row['date'] . "</date>\n";

        // Escaping illegal characters

        $row['text'] = str_replace("&", "&", $row['text']);

        $row['text'] = str_replace("<", "<", $row['text']);

        $row['text'] = str_replace(">", "&gt;", $row['text']);

        $row['text'] = str_replace("\"", "&quot;", $row['text']);

    $xml_output .= "\t\t<text>" . $row['text'] . "</text>\n";

    $xml_output .= "\t</entry>\n";

}

$xml_output .= "</entries>";

echo $xml_output;

?>

Inspiring
August 19, 2010

XML is indeed more solid, especially if your queries output contains special characters.

Just a couple of thoughts.

1. You don't need to place extra line breaks and paces into xml in PHP. First, they don't matter; second, XML will be smaller.

2. If you expect special (illegal) characters in output - you should use CDATA in the XML. This way there is no need for their replacement. For instance, the lines

// Escaping illegal characters

        $row['text'] = str_replace(&quot;&&quot;, &quot;&&quot;, $row['text']);

        $row['text'] = str_replace("<", "<", $row['text']);

        $row['text'] = str_replace(&quot;&gt;&quot;, &quot;&gt;&quot;, $row['text']);

        $row['text'] = str_replace(&quot;\&quot;&quot;, &quot;&quot;&quot;, $row['text']);

    $xml_output .= &quot;\t\t<text&gt;&quot; . $row['text'] . &quot;</text&gt;\n&quot;;

can be just one line:

$xml_output .= &quot;<text&gt;<![CDATA[&quot; . $row['text'] . &quot;]]&gt;</text&gt;&quot;;

Also, this is just my opinion, conceptually, I feel that PHP side shouldn't care for what is legal and what is not from a standpoint of how other application consumes the output (in this case Flash).  In addition, from the standpoint of content management, it is difficult to account for all the special characters and as such it makes data vulnerable to future bugs and data entry deviations. CDATA allows for presenting data in its purest form.

Inspiring
August 20, 2010

Excellent information as always. Thanks Andrei.

My Virtual World is really coming on. When it's up and running (stunning graphics) I'll send a link. You have helped me with a lot of it ith regards optimization etc... (I owe you).(Just gonna post a new one now on the canvass size.)

kglad
Community Expert
Community Expert
August 15, 2010

that's as2 code.

recheck your publish settings (file/publish settings/flash)

Inspiring
August 15, 2010

Excuse me. I didn't check when the tutorial was written.

I can google the question but there are hundreds of tuts and all a little different.

Could you possibly send me a link of the best/ easiest way to implement this.

Loadvars seems quite easy - would you recommend that instead of converting to xml?

kglad
Community Expert
Community Expert
August 15, 2010

you're confused.

your first decision is whether to use as2 or as3.  your second decision is whether to use a mysql database or use xml to supply data to your swf.

if you use as2, you'll use loadvars to load data from a mysql database or you'll use the xml class to load data from an xml file.

if you use as3, you'll use the urlloader class to load data from either.

if you decide on as2 and need help, post in the as1/as2 forum.