Discussion:
connecting mysql with coldfusion
(too old to reply)
bluPrince
2009-02-24 16:47:12 UTC
Permalink
I am trying to connect coldfusion with mysql database, but for some reason I am
not able to. mysql is on my local machine not on any serer, is there any way
where I can specify that? Part of my syntax is as follows:
<head>
<cfquery name="students" dbName= "students" dbServer ="localhost"
password="gateway">
mysql> select * from students;
</cfquery>
</HEAD>

Thanks!
Azadi
2009-02-24 17:09:51 UTC
Permalink
you need to create a Data Source Name (DSN) in CF Admin to connect to
your db. cf since looooooong ago does not support in-line connection
creation - only through DSN. then you specify the DSN to connect to in
<cfquery> tag's DATASOURCE attribute.

and secondly, inside cfquery tag you just type your SQL - do not put
'mysql >' there

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Tariq Ahmed - ACE
2009-02-25 04:28:47 UTC
Permalink
Another trouble shooting step if you can't get CF to connect directly to mySQL
via the CF Administrator's DataSource area, is you can create an ODBC
Datasource in Windows and test to make sure that works.

If you got the ODBC test-connection part working, you can then create a CF
Datasource that connects to that ODBC datasource. But that's to at least get
something working...

But you'd want to hammer out getting CF to connect to mySQL directly.
BKBK
2009-02-25 06:57:51 UTC
Permalink
Hi, BluPrince

The start often seems troublesome, but take heart. Let's start from scratch.

In the vast majority of cases, you may ignore HTML tags in your Coldfusion
page. Coldfusion and the webserver will put them in in the background before
sending the response to the browser. So you may just save the following code
as [i]myDatabaseConnectionTest.cfm[/i] in the web root. That is, in the wwwroot
folder of your Coldfusion installation.

<!--- Query students table. Datasource configured in Administrator--->
<cfquery name="getStudents" datasource="studentDSN">
select * from students
</cfquery>

<!--- Dump query result--->
<cfdump var="#getStudents#">

To configure Coldfusion for MySQL, follow these steps.

1) Open the Coldfusion Administrator -- usually by pointing the browser to:
http://localhost:8501/CFIDE/administrator/
Then go to the datasources page.

2) Enter/Select the following values
Data Source Name: studentDSN
Driver: MySQL(4/5)

Click on the [i]Add[/i] button. You should get configuration page for your new
datasource.

3) Enter the following values
Database: students
Server: 127.0.0.1 Port: 3306
Username: root
Password: gateway

Leave the [i]Description[/i] field empty. I have made these assumptions:
- you have MySQL 4.x or MySQL 5.x;
- you have created a database called [i]students[/i] (which contains the table
called [i]students[/i])
- your MySQL username is [i]root[/i] and your password is [i]gateway[/i]

4) Press [i]Submit[/i] for Coldfusion to create the datasource. Your new
datasource should appear in the list [i]Connected Data Sources [/i]

5) Assuming everything has gone well so far, close the browser (to leave the
Coldfusion Administrator).

6) Open the page [i]myDatabaseConnectionTest.cfm[/i] -- usually by pointing
the browser to:
http://localhost:8500/myDatabaseConnectionTest.cfm

Loading...