# Flash & MySQL

**URL:** <https://forum.kirupa.com/t/flash-mysql/27441>\
**Category:** programming\
**Created:** [August 2, 2003, 12:32pm UTC](https://forum.kirupa.com/t/flash-mysql/27441 "2003-08-02T12:32:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Voetsjoeba](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@Voetsjoeba](https://forum.kirupa.com/u/Voetsjoeba)\
**Post date:** [August 2, 2003, 12:32pm UTC](https://forum.kirupa.com/t/flash-mysql/27441/1 "2003-08-02T12:32:06Z")

</div>

Hi,

I’m pulling data from a MySQL database into Flash (MX) through PHP.

In MySQL, I have a table in the database “max\_voetsjoeba” called “news” in which there are three fields. A dummy value, “inhoud” and “datum”.

This is the code for the PHP file (thanks Jubba ;)):

```php

<?
$l = mysql_connect("localhost", "max_voetsjoeba","--hidden--");
mysql_select_db("max_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($r);

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

mysql_close($l);
?>

```

This works fine, you can see the output of this PHP file [here](http://voetsjoeba.asianillusion.com/news.php).

In Flash, I have:

[AS]  
onClipEvent (load) {  
lv = new loadVars();  
lv.onLoad = function(success) {  
if (success) {  
[//this.i](https://this.i) is the variable, i is a textfield  
i = this.i;  
inhoud = this.inhoud  
img.loadMovie(“[http://users.pandora.be/voetsjoeba/eclipse.jpg](http://users.pandora.be/voetsjoeba/eclipse.jpg)”);  
} else {  
trace(“failed to load script”);  
}  
};  
lv.load(“[http://voetsjoeba.asianillusion.com/news.php](http://voetsjoeba.asianillusion.com/news.php)”);  
}  
[/AS]

(thanks Ahmed ;))  
I can see the image file, but I can’t see the text (inhoud). I took a look at the PHP file and changed “this.inhoud” to “this.inhoud0” (see the PHP file’s output). This worked, but I wanted it to change automatically, so I tried “inhoud = this.inhoud+this.i” (i is 0, see PHP file’s output). This doesn’t work, instead of this.inhoud0 it becomes 0. Why ?

And how do I make the textfield scroll AND have it render as HTML ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 2, 2003, 1:15pm UTC](https://forum.kirupa.com/t/flash-mysql/27441/2 "2003-08-02T13:15:11Z")

</div>

try this:

```auto
lv = new loadVars();
        lv.onLoad = function(success) {
                if (success) {
                        //this.i is the variable, i is a textfield
                        i = this.i;
                        inhoud.html = 1
                        inhoud.htmlText = this["inhoud"+i]
                        img.loadMovie("http://users.pandora.be/voetsjoeba/eclipse.jpg");
                } else {
                        trace("failed to load script");
                }
        };
        lv.load("http://voetsjoeba.asianillusion.com/news.php");

```

and make sure you have a textfield with an instance name of ‘inhoud’… to make it scroll, add the scrollbar component onto the textfield 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 2, 2003, 5:42pm UTC](https://forum.kirupa.com/t/flash-mysql/27441/3 "2003-08-02T17:42:05Z")

</div>

It works ! Thanks ahmed ! =) If you don’t mind, I’ve got some additional questions :beam:

[See this](http://voetsjoeba.asianillusion.com/home.swf) – There’s much space between the lines. How can I fix this ?  
[EDIT]Nevermind … =)[/EDIT]

How can I make it so that, when I add a row to the MySQL database, Flash automatically places the content of the new row above all previous text ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 3, 2003, 8:29am UTC](https://forum.kirupa.com/t/flash-mysql/27441/4 "2003-08-03T08:29:49Z")

</div>

try this 🙂

```auto
onClipEvent (load) {
	inhoud.condenseWhite = true;
	lv = new loadVars();
	lv.onLoad = function(success) {
		if (success) {
			i = this.i;
			inhoud.html = 1;
			for (l=i; l>=0; l--) {
				inhoud.htmlText += this["inhoud"+l];
			}
			img.loadMovie("http://voetsjoeba.asianillusion.com/ca2small.jpg");
		} else {
			trace("failed to load script");
		}
	};
	lv.load("http://voetsjoeba.asianillusion.com/news.php");
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 3, 2003, 8:40am UTC](https://forum.kirupa.com/t/flash-mysql/27441/5 "2003-08-03T08:40:04Z")

</div>

[AS]for(i=0; i \<= 1000; i++){  
\_root.genius.ahmed.thanks += 1;  
}  
[/AS]  
:beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 3, 2003, 8:41am UTC](https://forum.kirupa.com/t/flash-mysql/27441/6 "2003-08-03T08:41:41Z")

</div>

haha 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 6, 2003, 2:48pm UTC](https://forum.kirupa.com/t/flash-mysql/27441/7 "2003-08-06T14:48:47Z")

</div>

Could you give me a hint on making a news thingie like the following…

In my frontpage I have a box in which I print 7 most recent news headlines from mySQL with PHP, sorted by date and limited to 7.

Ok. That works fine. What I would like to do is that when for example the first headline is clicked, I get the first news item opened. If the fourth headline is clicked tha fourth item is opened and so on.

That shouldn’t be too hard, but I can not get it working with Flash.

Anyone?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 6, 2003, 3:31pm UTC](https://forum.kirupa.com/t/flash-mysql/27441/8 "2003-08-06T15:31:46Z")

</div>

One advice:

in situations like this:  
lv.load(“[http://voetsjoeba.asianillusion.com/news.php](http://voetsjoeba.asianillusion.com/news.php)”);

if u want change to this:  
lv.load("./news.php");

Imagine if u have to change the domain … u will have to change and if u have with ./ no problem exist 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 6, 2003, 3:35pm UTC](https://forum.kirupa.com/t/flash-mysql/27441/9 "2003-08-06T15:35:43Z")

</div>

villek,

when user click in a item, u have to send a variable to php script and in script u have to verify what item the user have selected and change the SQL instruction with ORDER BY and send again to flash the new result

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 6, 2003, 4:55pm UTC](https://forum.kirupa.com/t/flash-mysql/27441/10 "2003-08-06T16:55:54Z")

</div>

So how would the code go? “ORDER BY newsID”, or something like that?

The MySql table has newsID(auto increment), author, posted, title and body -rows.

underneath is the code in use (from phpforflash, slightly modified):

```php

$link = @mysql_connect($dbHost, $dbUser, $dbPass);

if (!$link)
{
    print "&newsText=" . urlencode("Could not connect to server");
    exit;
}

if (!@mysql_select_db($dbName))
{

    print "&newsText=" . urlencode("Could not select $dbName database");
    exit;
}

$query = "SELECT * FROM MyNews ORDER BY posted DESC LIMIT 7";

$result = @mysql_query($query);

if ($result && @mysql_num_rows($result) > 0)
{

    $newsText = "";
    $otsikot = "";
    $pvm = "";

    while($row = mysql_fetch_array($result))
    {
	
				
        $posted = strftime("%a %d.%m.%y %H:%M", $row['posted']);

        
        $otsikot .= stripslashes($row['title']);
        $otsikot .= '<br><br>';
				
        $newsText .= stripslashes($row['title']);
        $newsText .= '</b></font><br>';

        $pvm .= $posted . " " . $row['author'];

        $newsText .= stripslashes($row['body']) . '<br><br>';
    }

print "&otsikot=" . urlencode($otsikot);

print "&pvm=" . urlencode($pvm);

print "&newsText=" . urlencode($newsText);

}
else
{

print "&otsikot=" . urlencode("No headlines, Bo!");

print "&newsText=" . urlencode("No news, Bo!");
}

mysql_close($link);

?>

```

I’m not quite sure how to tell flash that some exact textfield in a movieclip is supposed to print out the newsitem defined earlier.

[http://www.macromedia.com](http://www.macromedia.com) has the sort of thing I’m talking about on their front page. Every headline is a link and from a certain link, a certain newsitem is displayed. I just want the item to be displayed in flash.

Thank for the quick reply **Drunken**!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 6, 2003, 8:53pm UTC](https://forum.kirupa.com/t/flash-mysql/27441/11 "2003-08-06T20:53:09Z")

</div>

ok, I sort of got working. It works fine but not dynamically enough.

I fatched the max value of the newsID cell and in order to get for example the second latest news item i just decrement 1 from the max value and print my item out like that.

It sucks 'cause I have to code all the items separately and use separate php files or the “switch” -method or something. I’m really poor with php and Flash:(

Man, if someone out there has an idea on how to do this, pls share your thoughts on a bit more precise level…

Thanks in advance, these forums are really good and I greatly appreciate everyone here. Let’s keep up the work!
