php计数2参数错误

时间:2018-04-24 14:59:11

标签: php mysqli

我正在创建一个函数,它为我提供了数据库中满足2个条件的记录总和。

我这样开始

function cuentaticketspendienteempleado($conexion){

         $pendientes = (mysqli_query($conexion, "SELECT COUNT(*) AS conteo FROM ticket ")) or die("Error mostrando  tickets  pendientes: ".mysqli_error($conexion));
        $resultados = mysqli_fetch_row($pendientes);
        return $resultados[0];
    }

我需要在帐户中再添加一个字段。 我想分配的ID部门参数只告诉我该部门的门票或报告

using the previous function

当我尝试使用此功能在该部门中考虑该用户的门票时

function listTicketUnrevisedSupervisor($conexion, $id){

        $consulta = mysqli_query($conexion, "SELECT *, COUNT(t.id) as contador_tickets, t.id as id_ticket, u.id as user_id, t.fecha_creacion as t_fcreacion, t.hora_creacion as t_hcreacion
                                     FROM ticket as t
                                     JOIN usuario AS u
                                     ON t.id_usuario = u.id
                                     WHERE t.status <> '3' AND u.id_departamento = ".$id."") 
                                     or die("Error listando Ticket: ".mysqli_error($conexion));

        return $consulta;
    }

我明白了

error

1 个答案:

答案 0 :(得分:0)

也许只是帖子的格式,但是当您选择其他特定字段时,*无法在查询中使用,您可以使用此类函数

function listTicketUnrevisedSupervisor($conexion, $id) {

$query = "SELECT t.field1, u.field_2, t.id as id_ticket, u.id as user_id, "
        . "t.fecha_creacion as t_fcreacion, "
        . "t.hora_creacion as t_hcreacion "
        . "FROM ticket as t "
        . "JOIN usuario AS u ON t.id_usuario = u.id "
        . "WHERE t.status <> '3' "
        . "AND u.id_departamento = " . $id . " "
        . "ORDER BY t.id DESC ";   
if($consulta = mysqli_query($conexion, $query)){
   return $consulta; 
} else {
    die("Error listando Ticket: ".mysqli_error($conexion));
}    
}

或者只选择所有内容

function listTicketUnrevisedSupervisor($conexion, $id) {

$query = "SELECT * "
        . "FROM ticket as t "
        . "JOIN usuario AS u ON t.id_usuario = u.id "
        . "WHERE t.status <> '3' "
        . "AND u.id_departamento = " . $id . " "
        . "ORDER BY t.id DESC ";   
if($consulta = mysqli_query($conexion, $query)){
   return $consulta; 
} else {
    die("Error listando Ticket: ".mysqli_error($conexion));
}    
}

计算行数

$num_rows = mysqli_num_rows($consulta);

mysqli_query documentation (en)

Here you can check the documentation about mysqli_query (En Español Juan)

希望我的回答可以帮到你。

编辑,获取所有结果

您可以使用类似这样的程序来完成

    function getTheAssocArrayOfMyQuery($consulta){
    $return= array();
    while ($row= mysqli_fetch_assoc($consulta)) {
        array_push($return, $consulta);
    }
    return $return;
    }

或者,使用mysqli_fetch_all()

     $this_is_the_array_i_will_use = mysqli_fetch_all($consulta,MYSQLI_ASSOC);

mysqli_fetch_all() manual