So I have this problem that gives me headache. appreciate if anyone can help me solve this.
I’m using Coldfusion with MSSQL together with MySQL. The MSSQL database is the main datasource for this application that i’m working on right now. While MySQL is used for the employee database (it was created to develop employee’s database with PHP). I have to connect to it in order to make this application work.
The MySQL tables:
TBL_EMPLOYEE
+---------+------------------+-------------------|
|EMP_ID | EMP_NUMBER | DATE_OF_BIRTH |
+---------+------------------+-------------------|
| 1 | 00001 | 2009-01-01 |
| 2 | 00002 | 2009-01-15 |
| 3 | TEMP01 | 2009-05-10 |
| 4 | TEMP02 | 2010-02-04 |
| 5 | 0006 | 2010-03-01 |
+---------+------------------+-------------------|
TBL_CHILD
+---------+------------------+---------------------|
|EMP_ID | CHILD_ID | DATE_OF_BIRTH |
+---------+------------------+---------------------|
| 1 | 1 | 2008-11-12 |
| 1 | 2 | 2010-10-06 |
| 2 | 3 | 2009-05-10 |
| 5 | 4 | 2010-02-16 |
| 5 | 5 | 2012-03-08 |
+---------+------------------+---------------------|
This application will check a staff’s DOB. If he has kids, the system will display his eldest child’s DOB. Otherwise (if he doesn’t have kids) the system will display his own DOB.
Here is the problem:
<cfquery name="getEmployee" datasource="#mysqlDB#">
SELECT EMP_ID,EMP_NUMBER,DATE_OF_BIRTH
FROM TBL_EMPLOYEE
WHERE EMP_NUMBER = '#users.EMP_NO[mainRow]#'
</cfquery>
<cfset mysql_id = getEmployee.EMP_ID>
<cfquery name="getChild" datasource="#mysqlDB#">
SELECT *
FROM TBL_CHILD
WHERE EMP_ID =#mysql_id#
ORDER BY DATE_OF_BIRTH ASC
LIMIT 1
</cfquery>
Note :
#users.EMP_NO[mainRow]# --> loop query from MSSQL
EMP_NUMBER = string
EMP_ID = integer (auto)
This query works when the employee number is numbers (i.e: 001, 101, 23002, etc)
(without the ” symbol )
<cfquery name="getEmployee" datasource="#mysqlDB#">
SELECT *
FROM TBL_EMPLOYEE
WHERE EMP_NUMBER = #users.EMP_NO[mainRow]#
</cfquery>
<cfset mysql_id = getEmployee.EMP_ID>
<cfquery name="getChild" datasource="#mysqlDB#">
SELECT *
FROM TBL_CHILD
WHERE EMP_ID =#mysql_id#
</cfquery>
But it gives me error when employee number is character (TEMP101, TEMP007, etc).
'#users.EMP_NO[mainRow]#'
I have tried to use the same query in phpmyadmin, it works just fine. Can anyone tell me why is this happening and how to solve this?
(Update from comments)
The error message is:
Error Executing Database Query. You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near ” at line 3 SELECT * FROM TBL_CHILD WHERE
EMP_ID =
Actually the error is caused by the difference of EMP_NUMBER in MSSQL and MySQL.
eg:
so to solve this, what i do is just to trim the mysql_id
Thanks for all answers and am very sorry for the irrelevant question.