Question

I am working on ColdFusion9 and MySQL 5.0. I am not sure why the query name variables get undefined in the insert and update queries, even if I have declared those variables just before the query. Here is the sample code.

<cfset variables.test_update = QueryNew('')>
<cfset variables.res = ''>
<cfquery name="variables.test_update" datasource="dsnTest" result="variables.res">
    INSERT INTO test(
        name
        , rank
    )VALUES(
        'test'
        ,23
    )
</cfquery>
<cfdump var="#variables.res#">
<cfdump var="#variables.test_update#">

Here the result attribute value is available, but for the query name variable, it is throwing the undefined error, "Element TEST_UPDATE is undefined in VARIABLES."

I know that, the insert, update and delete queries do not return any result set through the query name attribute. But, I am not sure, why does that query name variable get undefined after the query execution?

Please help.

Was it helpful?

Solution

ColdFusion populates variables.test_update with whatever is returned from the DB driver, which in this case is [nothing].

And CF considers a null variable to be undefined. Always has.

OTHER TIPS

Further to what Adam says, if you wanted to return the new ID for the new record (if your table is setup to handle this), you could change it as follows:

INSERT INTO test(
    name
    , rank
)VALUES(
    'test'
    ,23
);
Select LAST_INSERT_ID() as newid;

if you dumped "variables.test_update", you would see its now not empty, returning one row, for the column "newid".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top