Skip to main content
BreakawayPaul
Inspiring
November 13, 2012
Question

My first attempt at using a query inside a cfscript tag

  • November 13, 2012
  • 2 replies
  • 1110 views

My first attempt at this is not going well.

Here's what I have:

<cfscript>

function calbox(topic) {

    queryService = new query();

    queryService.setDatasource("hepoffice");

    queryService.setName("getevents");

    queryService.addParam(name="topic",value="arguments.topic",cfsqltype="cf_sql_numeric");

    result = queryService.execute(sql="SELECT event_title" &

                                            ", event_desc" &

                                            ", event_start_date" &

                                            ", event_url" &

                                            ", exit_door" &

                                        "FROM events" &

                                        "INNER JOIN pagetopics" &

                                                "ON pagetopics.topic_id = events.event_topic" &                                   

                                        "WHERE pagetopics.topic_id = :topic");

}

</cfscript>

When I view a page that has this code block on it, I get the following error:

Invalid CFML construct found on line 29 at column 28.

ColdFusion was looking at the following text:

query

The CFML compiler was processing:

    A script statement beginning with queryService on line 29, column 9.

    A script statement beginning with function on line 28, column 1.

    A cfscript tag beginning on line 27, column 2.

This isn't where I expected to get an error.  Anyone have any idea where I've gone wrong?

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    November 14, 2012

    If you insist on cfscript, then you could also do it in Java. Here follows an example in MySQL. It is a quick job, and I have omitted niceties like try-catch.

    <cfscript>

        class = createobject("java","java.lang.Class");

        Class.forName("com.mysql.jdbc.Driver");

        driverManager = createobject("java","java.sql.DriverManager");

        connection = driverManager.getConnection("jdbc:mysql://localhost:3306/testDB", "myUserName", "myPassword") ;

        statement = connection.createStatement() ;

        query = "select * from animals";

        resultSet = statement.executeQuery(query) ;

        while ( resultSet.next() ) {

                   writeoutput(resultSet.getstring('id') & ' | ' & resultSet.getstring('species') & '<br>');        

            }

          resultSet.close();

          statement.close();

          connection.close();

    </cfscript>

    Inspiring
    November 13, 2012

    What verison of CF are you on?  You code compiles OK for me on CF 9.0.2, however I get your exact error on CF8.0.1. The service CFCs like Query.cfc weren't added to CF until CF9, I'm afraid.  And the new keyword wasn't either.

    --

    Adam

    BreakawayPaul
    Inspiring
    November 14, 2012

    That's the problem then.  We're on CF8.  Any way to pull off a query inside a cfscript tag in CF8?

    Inspiring
    November 14, 2012

    That's the problem then.  We're on CF8.  Any way to pull off a query inside a cfscript tag in CF8?

    It's very easy to write a wrapper function for a CFML tag: just create a <cffunction> which takes the arguments the tag does, and then use them to call the tag. It's made slighly more complex with <cfquery> as it can have sub tags, but that's not so hard to do.

    Indeed that's all that Query.cfc is.  You could even use that very one: grab it out of a CF9 install and plonk it in your CF8 install: it should work (I dunno if it uses any CF9-only code, but that'll be fairly clear fairly quickly).  That said, I dunno how that will go as far as licensing goes.

    But other than that, fortunately other people have already asked (and resolved) the same question you're asking: https://github.com/CFCommunity/CFScript-Community-Components

    That said: <cfquery> is a much nicer way of doing an inline query than any way that you'll be able to find that works in a CFScript-only environment. Do you need to write this code in script?

    --
    Adam