$common_query_string = "Select a.INC_KEY,a.STATE, a.FDID, a.INC_DATE, a.INC_NO, a.EXP_NO, concat(a.NUM_MILE,' ', a.STREET_PRE,' ', a.STREETNAME,' ', a.STREETTYPE,' ',
a.STREETSUF, ' ',a.APT_NO,' ', a.CITY,' ', a.STATE_ID,' ', a.ZIP5,' ', a.ZIP4) as address, a.latitude, a.longitude,
( 3959 * acos( cos( radians($lat) ) * cos( radians( a.latitude ) )
* cos( radians( a.longitude ) - radians($lon) )
+ sin( radians($lat) ) * sin( radians( a.latitude ) ) ) ) AS distance,
a.INC_TYPE, a.FF_DEATH, a.OTH_DEATH, a.PROP_LOSS , a.CONT_LOSS,(b.CODE_DESCR) as INC_CODE_DESC, (d.CODE_DESCR) as CAUSE_CODE_DESC";
$query = sprintf("$common_query_string ,'nfirs' as SOURCE
From
(Select * From incident
Where latitude > '%s' And latitude < '%s'
And longitude > '%s' And longitude < '%s' %s)
As a , codelookup b,codelookup d
Where a.INC_TYPE = b.code_value AND b.fieldid = 'INC_TYPE'
And a.CAUSE_IGN = d.code_value AND d.fieldid = 'CAUSE_IGN'
having distance < '%s'
", mysql_real_escape_string($minLat), mysql_real_escape_string($maxLat), mysql_real_escape_string($minLon), mysql_real_escape_string($maxLon), mysql_real_escape_string($query_condition), mysql_real_escape_string($radius));
问问题
168 次
3 回答
1
当您使用较旧的mysql_
方法时,您可以mysql_num_rows()
在执行查询后使用:
$result = mysql_query($query);
$count = mysql_num_rows($result);
旁注(mysql_
方法已被弃用)
我建议重构您的代码以使用推荐的mysqli_
或PDO
方法。您可以在两者中执行相同类型的查询,但更新将使您能够使用面向对象样式的代码、有助于提高可读性的准备好的语句,并进一步保护您的代码免受 SQL 注入。
这是与 相同的计数语法mysqli_num_rows()
:
$result = mysqli_query($dbLink, $query);
$count = mysqli_num_rows($result);
UPDATE(直接在查询中计算)
要直接在查询中获取总行数,可以使用 MySQL 的COUNT()
:
SELECT
COUNT(*) AS `count`
FROM
(Select * From incident Where latitude > '%s' And latitude < '%s' And longitude > '%s' And longitude < '%s' %s) As a, codelookup b,codelookup d
WHERE
a.INC_TYPE = b.code_value AND b.fieldid = 'INC_TYPE' AND a.CAUSE_IGN = d.code_value AND d.fieldid = 'CAUSE_IGN'
HAVING
distance < '%s'
于 2012-11-01T05:26:10.870 回答
1
mysql_num_rows($result);
为您提供查询结果的选定行数。
SELECT COUNT(*) FROM yourtable your join yourcondition;
使用 LIMIT 时获取总行数...
如果您使用了 LIMIT 子句但想知道没有它会得到多少行,请在查询中使用 SQL_CALC_FOUND_ROWS,然后是 SELECT FOUND_ROWS();
SELECT SQL_CALC_FOUND_ROWS FROM yourtable your join yourcondition
LIMIT 10;
SELECT FOUND_ROWS();
于 2012-11-01T05:27:29.063 回答
0
mysql_num_rows($result);
SELECT COUNT(*) FROM yourtable your join yourcondition;
you have to use this :
*$final_Query='select count(*)as count from ('.$query.')*
and palce this below
Where a.INC_TYPE = b.code_value AND b.fieldid = 'INC_TYPE'
And a.CAUSE_IGN = d.code_value AND d.fieldid = 'CAUSE_IGN'
having distance < '%s, mysql_real_escape_string($minLat), mysql_real_escape_string($maxLat), mysql_real_escape_string($minLon), mysql_real_escape_string($maxLon), mysql_real_escape_string($query_condition), mysql_real_escape_string($radius));
于 2012-11-01T06:08:17.313 回答