bsp; return iReturnNumRows;} //2.使用limit版int CDBManager::QueryHostCache(MYSQL * connecthandle, char * channelid, unsigned int myexternalip, int ISPtype, CHostCacheTable * hostcache){ //首先获取满足结果的记录条数,再使用limit随机选择指定条记录返回 MYSQL_ROW row; MYSQL_RES * pResultSet; char selectSQL[SQL_LENGTH]; memset(selectSQL, 0, sizeof(selectSQL)); sprintf(selectSQL,"select count(*) from HostCache where ChannelID = '%s' and ISPtype = %d", channelid, ISPtype); if(mysql_real_query(connecthandle, selectSQL, strlen(selectSQL)) != 0) //检索 return 0; pResultSet = mysql_store_result(connecthandle); if(!pResultSet) return 0; row = mysql_fetch_row(pResultSet); int iAllNumRows = atoi(row[0]); mysql_free_result(pResultSet); //计算待取记录的上下范围 int iLimitLower = (iAllNumRows <= RETURN_QUERY_HOST_NUM)? 0:(rand()%(iAllNumRows - RETURN_QUERY_HOST_NUM)); int iLimitUpper = (iAllNumRows <= RETURN_QUERY_HOST_NUM)? iAllNumRows:(iLimitLower + RETURN_QUERY_HOST_NUM); //计算待返回的结果数 int iReturnNumRows = (iAllNumRows <= RETURN_QUERY_HOST_NUM)? iAllNumRows:RETURN_QUERY_HOST_NUM; //使用limit作查询 sprintf(selectSQL,"select SessionID, ExternalIP, ExternalPort, InternalIP, InternalPort " "from HostCache where ChannelID = '%s' and ISPtype = %d limit %d, %d" , channelid, ISPtype, iLimitLower, iLimitUpper); if(mysql_real_query(connecthandle, selectSQL, strlen(selectSQL)) != 0) //检索 return 0; pResultSet = mysql_store_result(connecthandle); if(!pResultSet) return 0; //获取逐条记录 for(int i = 0; i<iReturnNumRows; i++) { //获取逐个字段 row = mysql_fetch_row(pResultSet); if(row[0] != NULL) &nb