0

我正在尝试在 ColdFusion 中创建一个脚本,该脚本通过使用以下查询来检测指定用户是否存在查询:

            // User one is defined by the current session
            <cfquery name = "Friendship" datasource = "#DSN#">
            SELECT * 
            FROM  `conversations` 
            WHERE  `user_one` LIKE  '#user.id#'
            ORDER by id ASC
            </cfquery>

          <cfquery name = "FriendshipToUsername" datasource = "#DSN#">
            SELECT *
            FROM users
            WHERE id = '#user_two#'
            ORDER BY id DESC
          </cfquery>

但是,当此查询没有结果时,我想使用以下查询:

            <cfquery name = "Friendship" datasource = "#DSN#">
            SELECT * 
            FROM  `conversations` 
            WHERE  `user_two` LIKE  '#user.id#'
            ORDER by id ASC
            </cfquery>

          <cfquery name = "FriendshipToUsername" datasource = "#DSN#">
            SELECT *
            FROM users
            WHERE id = '#user_one#'
            ORDER BY id DESC
          </cfquery>

在 ColdFusion 中可以做到这一点吗?我想以这种方式保留查询的名称,否则我将需要更改我的整个站点。这是我页面的代码,如果您需要它:

  <cfif IsDefined("FriendshipToUsername.username")>

            <div class="person">
                <div class="avatar">
                    <div style="z-index:1;width: 64px; height: 73px; margin-bottom:-20px; margin-top:-30px; float: right; background: url(http://habbo.it/habbo-imaging/avatarimage?figure=#FriendshipToUsername.look#&amp;action=wav&amp;direction=3&amp;head_direction=3&amp;gesture=srp&amp;size=b&amp;img_format=gif);"></div>

                </div>
                <div class="info">
                  <a href="chat.cfm?chat=#Friendship.id#" style="text-decoration: none; color: ##4b4b4b;">  <h3>#FriendshipToUsername.username#</h3></a>
                    <a href="chat.cfm?chat=#Friendship.id#" style="text-decoration: none; color: ##4b4b4b;"><p>#FriendshipToUsername.motto#</p></a>
                    <p>Laatst gezien: gisteren</p></a>

                </div>
            </div>
            </cfif>

提前致谢!

4

3 回答 3

4

您可以测试该.recordcount属性,如下所示。

<cfif friendship.recordcount eq 0>

     <cfquery name = "Friendship" datasource = "#DSN#">
      SELECT * 
      FROM  `conversations` 
      WHERE  `user_two` LIKE  <cfqueryparam cfsqltype="cf_sql_varchar" value="#user.id#">
      ORDER by id ASC
     </cfquery>

     <cfquery name = "FriendshipToUsername" datasource = "#DSN#">
     SELECT *
     FROM users
     WHERE id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#user_one#">
     ORDER BY id DESC
     </cfquery>

</cfif>

我还建议查找并使用 cfqueryparam 标记来防止您的代码受到 sql 注入的影响。

于 2013-04-02T14:12:34.283 回答
1

我建议稍微修改您的原始查询,以便它尝试同时检索两条记录。这样,如果第一个查询返回零行,您就不需要两次访问服务器:

<cfquery name = "Friendship" datasource = "#DSN#">
    SELECT * 
    FROM  `conversations` 
    WHERE  `user_one` LIKE  '#user.id#'
    OR [get other user here]
    ORDER by id ASC
</cfquery>

执行查询后,只需拉出适当的用户。

于 2013-04-02T13:57:54.270 回答
1

检查和查询的recordcount属性。您可以简单地使用与以前相同的查询名称重新查询数据库。friendshipfriendshipToUsername

当然,您还应该使用cfqueryparam来保护您的数据库免受 sql 注入尝试的影响。

<!---(original friendship and friendshipToUsername queries)--->
<cfif NOT friendship.recordcount>   <!--- or "friendship.recordcount EQ 0" if you prefer --->
  <cfquery name = "Friendship" datasource = "#DSN#">
    SELECT * 
    FROM  `conversations` 
    WHERE  `user_two` LIKE  <cfqueryparam value="#user.id#" cfsqltype="cf_sql_integer">
    ORDER by id ASC
  </cfquery>

  <cfquery name = "FriendshipToUsername" datasource = "#DSN#">
    SELECT *
    FROM users
    WHERE id = <cfqueryparam value="#user_one#" cfsqltype="cf_sql_integer">
    ORDER BY id DESC
  </cfquery
</cfif>   
于 2013-04-02T14:19:50.410 回答