Tuesday, July 15, 2008

Adding New User to MySQL

I had a tough time getting this to work, so I'm putting it here. To add a new user to a MySQL database:

CREATE USER 'username'

then:

GRANT SELECT, INSERT
ON 'database'.*
TO 'username'
IDENTIFIED BY 'password'

This creates a user named username and gives them Select and Insert privileges to the database called database. They will need to log in with the password password.

To log in to MySQL as this user:

mysql -u username -p

This will prompt them for their password as assigned above.

Adding Database via ColdFusion to Flex project

Steps to add a new database to a Flex project:

1. Open the ColdFusion Admin page. Its probably here: http://localhost/CFIDE/administrator/index.cfm

2. Click Data Sources on left side.

3. In the Data Source Name field, give the new db a name. You will refer to this name in the ColdFusion Component datasource property.

4. In the settings, the Database field is the name of your database in MySQL. The username and password is the user/pw associated with that database in MySQL. If you need to add a new user in MySQL, follow the steps in the post called "Adding a New User in MySQL."

5. In Flex, you will refer to the ColdFusion component like this:

< id="requestName" destination="ColdFusion" source="componentFolder.cfcName">>
< /mx:RemoteObject >


For example, in the Inetpub\wwwroot folder I have a folder called components. Inside of here, I have a ColdFusion component called users.cfc. The users.cfc component contains a method called getUsers(). The RemoteObject call for this would be:

< id="userRequest" destination="ColdFusion" source="components.users">
< name="getUsers" result="returnHandler(event)">
< /mx:RemoteObject >



To initiate a call to this RO use: userRequest.getUsers();

The event handler for the results would be something like:

private function returnHandler(e:ResultEvent):void
{
var ac:ArrayCollection = new ArrayCollection();
ac = e.result as ArrayCollection;
}

And finally the getUsers() method in the CFC would look like this:

< name="getUsers" returntype="query">
< name="allUsers" datasource="dataSourceName">
SELECT * FROM users
< /cfquery >
<>
< /cffunction >



If anyone else is reading this, please note this is a very basic example and I'm only putting it here in case I forget how to do this in the future. I can't guarantee it is correct. :-)


Also note, the formatting keeps getting really messed up with the <<>> and some CF function names are being stripped, so hopefully its all clear.