我正在创建一个函数,它为我提供了数据库中满足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部门参数只告诉我该部门的门票或报告
当我尝试使用此功能在该部门中考虑该用户的门票时
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;
}
我明白了
答案 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);