sql:如何将查询结果计数直接存储到信息框中

时间:2012-10-31 06:31:10

标签: php mysql

我有一个信息框来显示事件的数量:

我有一个功能:

function getStateCounts()
{
    $query = sprintf("select t1.count, t2.State_name
        from (select count(*) as count, state
            from INC_DIST_SUMMARY S
                join SHAPE_LAYERS L on L.geo_id = S.shape_geo_id
            group by state) as t1, SHAPE_LAYER_STATE_DESC as t2 
            where t1.state=t2.state");

    $count = mysql_query($query);

    if (!$count) {
        echo "<error> Invalid query " . $query . "</error>"
        die("Invalid query: " . mysql_error());
    }

    return $count;
}

我必须将输出查询结果存储在“No of Thidents”中;

while ($item = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<item><title geoid='" . $item['GEO_ID'] . "'>" .
         $item['STATE_NAME']."(".($item['STATE_ABBR']).")" .
         "</title><description cong_dist='".$item['DIST_NAME']."'>";
    echo $item['STATE_ABBR'] . $item['DIST_NAME'] . " &lt;br/&gt";" **No of Incidents:";**
    echo $item['INC_COUNT'] . "</description>";
    echo "</item>"

我必须将此事件值修复为“No of Thidents”:

while ($item = mysql_fetch_array($count, MYSQL_ASSOC)) {
    echo "<summary>";
    echo "<state>".$count['STATE']."</state>";
    echo "<count>".$count['COUNT']."</count>" 
    echo "</summary>";
}

如何在“No of Tridents”变量中显示查询值?

1 个答案:

答案 0 :(得分:1)

在非COUNT(*)查询上执行rowCount();

或者执行mysql(i)_num_rows

标记您使用$ rowscounted的任何技术的结果并替换

if (!$count) {
            echo "<error> Invalid query " . $query . "</error>";
            die("Invalid query: " . mysql_error());
        }

        return $count;
}

使用:

if ($rowscounted == 0) {
    echo "<error> Invalid query " . $query . "</error>";
        die("Invalid query: " . mysql_error());
    }
else {
echo '$rowscounted states were returned by your query.'
}

我是php的新手,所以也许不是最好的答案,但我最近自己做了一个这样的问题,并认为这对你有用。 (作为一个新手,拿一小撮盐,哦,哈希,如果你愿意......)