Skip to main content
Participant
February 24, 2009
Question

connecting mysql with coldfusion

  • February 24, 2009
  • 3 replies
  • 1700 views
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!
This topic has been closed for replies.

3 replies

BKBK
Community Expert
Community Expert
February 25, 2009
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 myDatabaseConnectionTest.cfm 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:8500/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 Add button. You should get the 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 Description field empty. I have made these assumptions:
- you have MySQL 4.x or MySQL 5.x;
- you have created a database called students (which contains the table called students)
- your MySQL username is root and your password is gateway

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

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

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


Participating Frequently
February 25, 2009
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.
Inspiring
February 24, 2009
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/