使用函数显示Postgres中Select Count的数据

时间:2013-07-18 21:08:53

标签: php postgresql select

我正在尝试在这里做一个选择,但是我们没有显示任何数据,如下所示,我需要对postgre中的表进行选择计数,其中显示选择此字段的记录数不为空,只有多少记录非零。 已经有一个类似的功能,但它有所有的记录,我没有这样做只告诉记录非零。代码如下: 这是为用户显示的内容:

    <?php
    require_once 'chklogado.php';
    require_once 'model.php';
    $funcao = $_SESSION['funcao'];
    $model = new Model();
    $cont = array();
    $cont['promocoes'] = $model->get_count('promocoes');
    $cont['promocoes_novos'] = $model->get_promonovos('promocional');
    $cont['seminovos'] = $model->get_count('seminovos');
    $cont['noticias'] = $model->get_count('noticias');
    $cont['news'] = $model->get_count('contatos_newsletter');
    $cont['banner'] = $model->get_count('banner');
    $cont['pwd'] = $model->get_count('passwd');
    ?>

    <ul>

   <?php if($funcao<4 ) { // todos menos marketing?>
   <li class="inovos"><a href="promocoes-novos.php" title=""><span>Promoções de Carros Novos</span></a><span class="numberMiddle"><?php echo get_value($cont, 'promocoes_novos'); ?></span></li>
   <?php } //endif ?>

   <?php if($funcao==1 or $funcao==3 ) { // administrador e vendas ?>
   <li class="iOrders"><a href="lista-promocoes.php" title=""><span>Promoções</span></a><span class="numberMiddle"><?php echo get_value($cont, 'promocoes'); ?></span></li>
   <?php } //endif ?>

   <?php if($funcao==1 or $funcao==4 ) { // administrador e marketing ?>
   <li class="iStat"><a href="lista-banners.php" title=""><span>Cadastro Banners</span></a><span class="numberMiddle"><?php echo get_value($cont, 'banner'); ?></span></li>
   <?php } //endif ?>

   <?php if($funcao<4 ) { // todos menos marketing?>
   <li class="iMes"><a href="lista-seminovos.php" title=""><span>Add Seminovo</span></a><span class="numberMiddle"><?php echo get_value($cont, 'seminovos'); ?></span></li>
   <?php } //endif ?>

   <?php if($funcao==1 or $funcao==4 ) { // administrador e marketing ?>
   <li class="inoticias"><a href="lista-noticias.php" title=""><span>Notícias</span></a><span class="numberMiddle"><?php echo get_value($cont, 'noticias'); ?></span></li>
   <?php } //endif ?>


   <?php if($funcao==1 or $funcao==4 ) { // administrador e marketing ?>
   <li class="iNews"><a href="lista-newsletter.php" title=""><span>Mala direta</span></a><span class="numberMiddle"><?php echo get_value($cont, 'news'); ?></span></li>
   <?php } //endif ?>


   <?php if($funcao==1 ) { // administrador  ?>
   <li class="iUser"><a href="lista-usuarios.php" title=""><span>Admin Usuário</span></a><span class="numberMiddle"><?php echo get_value($cont, 'pwd'); ?></span></li>
   <?php } //endif ?>
</ul>

这是执行选择的功能:

function get_count($table = ''){
    return $table ? pg_fetch_result($this->get_all($table, '', 'COUNT(*) as total'), 0, 'total') : 0;
}

function get_promonovos($table = ''){
    //return $result=pg_query($sql="SELECT count(venda) FROM promocional where venda>0;");
    //$sql=("select * from promocional where venda is not null;");
    //$resultado = pg_fetch_array($sql);
    //return pg_free_result($resultado);
    //$data = pg_Fetch_Object($result, 0);
    //return pg_fetch_array($result, $sql);
    //return pg_free_result($result);

    $result = pg_query("select count(*) from promocional where venda is not null;");

    $count = pg_fetch_result($result);
}

你可以看到函数“get_Count”生成帐户,这个函数“get_promonovos”是我需要显示select的结果但是在这里测试或者他返回null或“Release id#”。 有谁知道会是什么?

2 个答案:

答案 0 :(得分:1)

管理解决只是改变了这样的功能:

$sql = "SELECT COUNT(venda) AS TOTAL FROM promocional WHERE venda is not null";
$query = pg_query($sql);
while($row = pg_fetch_array($query)){
$total = $row["total"];
}
echo $venda. $total;

答案 1 :(得分:-1)

Count(*)可能会更慢

  

$ sql =“select * from TableName”;

     

$ res = pg_query($ sql);

     

$ count = pg_num_rows($ res);

     

echo“#of Rows =”。$ count;