Subscribe to RSS
get email updates
home | about | pixDif AIR app | video tutorials
polyGeek.com

place your ad here

Web Premium



Get Qwest High Speed Internet



AMFPHP video tutorial : Creating and connecting to a MySQL database

August 6th, 2008 . by polygeek

pgTV

Using AMFPHP to connect to a MySQL database

Most of this tutorial is concerned with phpMyAdmin to create a database, table and user. Then we’ll write a PHP service that will connect and read/write data to the table we created. You can see an example of what we’ll create in the Flex app below

[ Other AMFPHP video tutorials ]

One thing that I meant to cover but spaced was creating a dbConnection.php file that holds your mysql_connect statements. Instead of leaving those statements in the constructor like I did here:

function Blogroll() { // this is the constructor for the 'Blogroll' class
   $mysql = mysql_connect( "localhost", "myUsername", "myPassword" );
   mysql_select_db(  "myDatabase" );
}

You can put that statement in an external file, I call mine dbConnection.php. That way you can refer to that file in all your services and if you need to update your connection statement you’ll only need to access one file. Plus, presumably, it’s more secure.

So in the end your constructor would look like this:

function Blogroll() {
   include("dbConnection.php");
}

And your connection file would look like this:

<?php
   $mysql = mysql_connect( "localhost", "myUsername", "myPassword" );
   mysql_select_db(  "myDatabase" );
?>

Please comment below if you have any input on how this video tutorial could be better. I’ll re-record this tutorial when I feel that I’ve gotten enough feedback to make some quality improvements.

view source

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

20 Responses to “AMFPHP video tutorial : Creating and connecting to a MySQL database”


comment number 1 by: Allen Scheinhaus

One thing I would point out is that when data typing in MySQL to VARCHAR, just use 255 as the length which is the max, unless you explicitly want to limit to a certain amount of characters. And well if its going to be larger than 255 characters use TEXT instead.

I think it is important for those new to AMFPHP to watch out do a little research of the number data type when storing numbers. This is often tricky and can lead to some headaches if not address properly before having an application go public.

For any large application in Flex/Amfphp using over 2 php services I would recommend to create a Connection.php class as a base and your service classes would extend it. Like this

include(“path/to/Connection.php”);
class MyServicesClass extends Connection { }

Here is an example

That way you can call the connect method from any Service Class you make and be connected, and you could also add other methods that will be used through the application. This avoids repetitive code and keeps every nice and tidy.

comment number 2 by: Allen Scheinhaus

class Connection
{
function Connection()
{
require_once(“/path/to/dbcon.php”);
$con = mysql_connect($db['host'], $db['user'], $db['pass']) or trigger_error(“Connection Failed: ” . mysql_error());
mysql_select_db($db['name'], $con) or trigger_error(“Connection Failed: ” . mysql_error());
}
}

comment number 3 by: starprog

Great tutorial. Just was wondering though how could I get a hold of the php file, I could not find it anywhere.

comment number 4 by: polyGeek

@starprog, sorry about that. I have added it to the source download. Click -> view source -> download source. ( bottom-left ).

comment number 5 by: starprog

No prob, I built my own anyways, just had to pause your video to copy the methods. Learn better that way anyways. Excellant work by the way. And those lines you get mad at, they are not as near annoying as you think especially when the content you are putting out is your quality. Outstanding work, and I want to tell you how much you are appreciated, thanks for giving to the community.

comment number 6 by: polygeek

@Starprog, You know, it’s comments like those that drive me to create more and more video tutorials. Stop it. I don’t have time . . . okay, I’ll do some more. :-)

Seriously, thanks for the great feedback. It really does motivate me to produce more for the community.

I’m also creating a new video player and recording videos at 1024×768 from now on. No more lines!

comment number 7 by: starprog

Well polygeek not trying to make your head swell or you work harder, but I believe its people like you that make learning software engineering easier. I believe I am speaking for just about everyone, even the ones that just take and not give back. (Don’t get mad if its not you). But people like yourself is what makes the programming community so great. So thanks again and keep up the good work.
btw: I just started my own blog to help give back, I am not near as far as you on your knowledge in flex, but Ill try to keep up. http://www.thingsiknowabout.com/wordpress/

comment number 8 by: polygeek

@starprog, Kudos to you for starting a blog about your Flex development. One thing I’ve discovered is that you don’t have to be a guru to blog about some bit of code. Most of the time I blog about things that I just picked up. I figure that if I just figured it out then it’s probably pretty clear to me how to communicate that to someone else. Where if I tried to write about it months later I’d foul it up somehow.

One of the primary reasons I blog is to remind myself how I did something. You have no idea how many times I’ve Googled my own site. It’s much easier than digging through projects.

Oh, and I added you to my RSS so I’ll be keeping up with your posts. Thanks!

comment number 9 by: Michel

Hey man! Thanks for this video. I was looking the internet for some posts about AMFphp and FLEX 3. Couldn’t find any until i found this one from you. It’s really helping me out here! Someday soon i’ll post what you learned me here in english on my own dutch blog! (in my own words ofcourse with a link to here ;-))

Hope to see some more in the near future. Maybe you could go into the flex side of the story a little deeper?
Or split the turorial in 2 movies. One for the PHP part and one for the Flex part.

comment number 10 by: polygeek

@Michel, Good timing, I plan on re-recording these videos today/tomorrow.

comment number 11 by: Michel

Hey,

If you ask me you dont have to send any “event” parameters with the function gotAddBlogrollResult( )… it works perfect without since your not using it.

Something minor, but i’m learning so i went searching for a little optimization and comments so i will never forget how it works. The only thing i could find was this :D

Greetz,
Michel

Michels last blog post..Flex 3 gratis, gratuit, free!

comment number 12 by: polygeek

@Michels, true, passing the event isn’t needed.

comment number 13 by: Zayd

Hey polygeek. I just got AMFPHP sorted with Flash CS3 and I was looking for an introduction to database handling. Just wanted to say I’ve added you to me blogroll and I’m impressed with the tutorial.

It’s kind of amazing, how people search the Internet, using the words they are trying to learn, and come across a site like yours, where in fact, you explain in a nice and easy manner!

Thanks again and I look forward to more of your tutorials!

Zayds last blog post..ActionScript 3.0

comment number 14 by: polygeek

@Zayd, thanks for the props. I really appreciate it. It’s comments like these that encourage me to make more. Which is the goal.

comment number 15 by: Aaron

I've just finished the first three videos and I thank you for publishing them.

I got lost though between 2 and 3 because video two ends with sending "AMFPHP ROCKS" via php. Three starts with the Blogroll application mxml already created, including the methods and functions (getBlog, addBlog etc..) that weren't explained previously. That's the magic I'm missing.

Best,

Aaron

comment number 16 by: polyGeek

@Aaron, sorry I missed a step or two in there. Were you able to get the blog roll app working? If so then you can probably figure out what's going on. Let me know if you have any questions.

comment number 17 by: Vladimir

Hi, could you make a tutorial about how to filter a datagrid (that get data from a DB) in flex using a ComboBox.

peace out!

comment number 18 by: polyGeek

@Vladimir, Take a look at my posts about Flash Platform Developer Densities. The data doesn't come from a DB but that really doesn't matter to the DataGrid. In the DataGrids there the dataSource is changed when the value of a NumericStepper changes. A ComboBox would work almost exactly the same. The code is pretty well commented. Let me know if that works for you.

comment number 19 by: Rene Roberto Ramirez Szabo

Great Tutorial.

Thx 4 all.

Venezuela – Caracas.

Blog: http://www.developyourdream.net

comment number 20 by: demonzorro

Thank you very much that was very helpful!

   Welcome back (Change)

Leave a Reply

comment feed RSS   subscribe to this comment thread

Recent Posts

   



polyGeek.com

© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites
Local Directory for Los Angeles, CA

Better Tag Cloud