MySQL如果没有结果

时间:2013-04-02 13:51:20

标签: coldfusion

我正在尝试在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>

提前致谢!

3 个答案:

答案 0 :(得分: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注入的影响。

答案 1 :(得分: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>

执行完查询后,只需拔出相应的用户即可。

答案 2 :(得分:1)

检查recordcountfriendship查询的friendshipToUsername属性。您可以简单地重新查询数据库,重用与以前相同的查询名称。

当然,您还应该使用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>