Warning: mysql_fetch_array () expects parameter 1 to be resource, boolean given in FILE on line 22

3

Why does it generate an error on line 22 if I compare it with other PHP and I have it right?

On line 22 I have the following:

while($rowHorEmp = mysql_fetch_array($resHorEmp))

And the variable $ resHorEmp comes from the following query:

$sqlHorEmp = "SELECT t.Nombre, t.Documento, t.Dia, MAX(t.Entrada) Entrada, MAX(t.Salida) Salida, t.Horario FROM ( SELECT u.Nombre, u.Documento, r.Dia, IF(r.Tipo = 'Entry', r.Hora, NULL) Entrada, IF(r.Tipo = 'Exit', r.Hora, NULL) Salida, CONCAT(h.Entrada, "-", h.Salida) Horario FROM USUARIOS u INNER JOIN relacion_colaborador_horario rh ON (u.Id_Usuario = rh.Id_Usuario) INNER JOIN horarios h ON (rh.Id_Horario = h.Id_Horario) INNER JOIN ( SELECT Documento, Tipo, Dia, IF(Tipo = 'Entry', MIN(Hora), MAX(Hora)) Hora FROM REGISTROS GROUP BY Documento, Tipo, Dia ) r ON(r.Documento = u.Documento) WHERE (r.Tipo = 'Entry' AND r.Hora > h.Entrada) OR (r.Tipo = 'Exit' AND r.Hora < h.Salida) ) t GROUP BY t.Nombre, t.Documento, t.Dia, t.Horario ORDER BY t.Dia DESC, t.Nombre ASC";

$resHorEmp = mysql_query($sqlHorEmp,$con);
    
asked by FOX 10.08.2017 в 23:42
source

2 answers

5

The problem is caused by the quotes in the query. Within the query you have "-" , it should be '-' .

The code would be this:

$sqlHorEmp = "SELECT t.Nombre, t.Documento, t.Dia, MAX(t.Entrada) Entrada, MAX(t.Salida) Salida, t.Horario FROM ( SELECT u.Nombre, u.Documento, r.Dia, IF(r.Tipo = 'Entry', r.Hora, NULL) Entrada, IF(r.Tipo = 'Exit', r.Hora, NULL) Salida, CONCAT(h.Entrada, '-', h.Salida) Horario FROM USUARIOS u INNER JOIN relacion_colaborador_horario rh ON (u.Id_Usuario = rh.Id_Usuario) INNER JOIN horarios h ON (rh.Id_Horario = h.Id_Horario) INNER JOIN ( SELECT Documento, Tipo, Dia, IF(Tipo = 'Entry', MIN(Hora), MAX(Hora)) Hora FROM REGISTROS GROUP BY Documento, Tipo, Dia ) r ON(r.Documento = u.Documento) WHERE (r.Tipo = 'Entry' AND r.Hora > h.Entrada) OR (r.Tipo = 'Exit' AND r.Hora < h.Salida) ) t GROUP BY t.Nombre, t.Documento, t.Dia, t.Horario ORDER BY t.Dia DESC, t.Nombre ASC";
    
answered by 11.08.2017 / 00:33
source
3

This is probably due to an error in the query stored in the variable $sqlHorEmp , since mysql_query returns FALSE in case of error.

To find out what is wrong, you can do the following:

$resHorEmp = mysql_query($sqlHorEmp,$con_mysql);
// Si la consulta fallo
if (!$resHorEmp) {
   die(mysql_errno($con_mysql) . ":" . mysql_error($con_mysql));
}

Where:

  • mysql_error : Returns the error message text of the previous MySQL operation.
  • mysql_errno : Returns the numeric value of the error message of the last operation MySQL .

-

PD : The MySQL extension was declared obsolete in PHP 5.5.0 and deleted in PHP 7.0.0 . Instead the extensions should be used MySQLi or PDO_MySQL .

    
answered by 10.08.2017 в 23:56