我编写了一个数据库查询来提取ColdFusion中的信息,我想知道如何将值传递给WHERE子句以获取相关数据。这是我的代码示例。任何人都可以帮忙吗?
<cfquery name="FILM_STRIP_QUERY" datasource="#dsn#">
select distinct tm.id as teachingmoduleid,
(select concat(prs.first_name, ' ',prs.last_name) AS Video_presenter from presentations pss
inner join topics tpcs on tpcs.id = pss.topic_id
inner join presenters prs on prs.id = pss.presenter_id
where pss.name = ps.name
and tpcs.title = tp.title
) AS video_presenter,
(select pss.43_png from presentations pss
inner join topics tpcs on tpcs.id = pss.topic_id
inner join presenters prs on prs.id = pss.presenter_id
where pss.name = ps.name
and tpcs.title = tp.title) AS png_name
from teaching_modules tm
inner join tm_segments sg on sg.module_id = tm.id
inner join topics tp on tp.id = sg.topic_id
inner join presenters prs on prs.id = tm.presenter_id
left outer join presentations ps on ps.id = sg.presentation_id
where tm.id =
</cfquery>
这是调用函数
<cfloop = "FILM_STRIP_QUERY">
<!--- this is where I wanna pass the parameter--->
</cfloop>
答案 0 :(得分:5)
你的意思是这样吗?
<cfset tmId = 5 />
<!--- or something like <cfset tmId = url.id /> --->
<cfquery name="FILM_STRIP_QUERY" datasource="#dsn#">
<!--- SELECT cols FROM wherever etc... --->
WHERE tm.id = <cfqueryparam cfsqltype="cf_sql_integer" value="#tmId#" />
</cfquery>
你可以在没有CFQueryParam标签的情况下做#tmid#,但最好使用它来增加安全性(验证),数据库也会缓存执行计划,希望在下次执行查询时提高性能。 / p>
答案 1 :(得分:3)
如果您正在使用CFC,那么这样的函数将起作用,包括确保CF从本地变量声明释放内存的查询名称。还使用参数和cfqueryparam函数。
<cffunction name="getFILM_STRIP" access="public" returntype="query" output="false">
<cfargument name="id" required="Yes" type="numeric">
<cfset FILM_STRIP_QUERY = "">
<cfquery name="FILM_STRIP_QUERY" datasource="#variables.dsn#">
<!--- select statement --->
WHERE colname = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value=#arguments.id# />
</cfquery>
<cfreturn FILM_STRIP_QUERY>
</cffunction>
答案 2 :(得分:2)
您应该使用cfqueryparam标记来执行此操作。这有助于数据库执行,也有助于防止SQL注入。 e.g。
where tm.id = <cfqueryparam value="#form.ID#" CFSQLType="CF_SQL_INTEGER">