查询使用php只获取一个整数值

时间:2014-03-21 18:23:12

标签: php mysql

嘿,我只想要一个只打印计数查询结果的查询。放入其他部分

          <?php


            $con = mysql_connect('localhost', 'root', '') or die(mysql_error());
            $db = mysql_select_db('quiz', $con) or die(mysql_error());
            $q="count(*) from question where ans=uanswer";
            $rq=mysql_query($q,$con);
            if(!$rq)
            {
            echo " the sql query faiiled to work ";
            }
            else
            {

            }

            ?>

1 个答案:

答案 0 :(得分:0)

您可以使用函数mysql_fetch_row()将数据库查询中的行作为数组返回。然后,您可以访问count(*)的结果作为返回数组中的第一个元素。

<?php

         $con = mysql_connect('localhost', 'root', '') or die(mysql_error());
         $db = mysql_select_db('quiz', $con) or die(mysql_error());
         $q="count(*) from question where ans=uanswer";
         $rq=mysql_query($q,$con);
          if(!$rq)
         {
             echo " the sql query faiiled to work ";
         }
         else
         {
             $row = mysql_fetch_row($rq);
             echo $row[0];
          }

    ?>