Javierh
Javierh

Reputation: 1

Problem executing a SQL query in PHP

I have this code, but the first query doesn't run (at phpmyadmin works!), I tried to run the code in 2 servers (maybe the config of php/mysql) but the results are the same.

$habitaciones = "SELECT habitacion.id AS habid, habitacion.nombre AS habnom, tipo.num_cama AS cantidad FROM habitacion, tipo WHERE id_tipo = tipo.id";
$enviar_sql = mysql_query($habitaciones, $enlace);

while($mostar_habs = mysql_fetch_array($enviar_sql)){
    echo "<table><tr>";

    $habid = $mostrar_habs['habid'];
    $habnom = $mostrar_habs['habnom'];
    echo "valor de habid: " .$habid;

    if($mostrar_habs['cantidad'] == 1){
        $i = 0;
        echo "<td>" . $habnom . "</td>";
        $fecha = $fechas[$i];

        $ocupacion1 = "SELECT cama.id AS camaid, cliente.nombre AS nombre, cama.ocupada AS ocupada FROM cliente, evento, cama, habitacion 
        WHERE cliente.id = id_cliente AND id_habitacion = habitacion.id AND cama.id = id_cama AND habitacion.id = " . $habid . " 
        AND checkin = \"" . $fecha . "\"";
        $enviar_ocupacion1=mysql_query($ocupacion1, $enlace);

        for($cliens=1; $mostrar_clien = mysql_fetch_array($enviar_ocupacion1); $cliens+=1){
            echo "<td>" . $mostrar_clien['nombre'] . "</td>";
        }
        $i++;
    }
    else{

        $i = 0;
        echo "<td>" . $habnom . "</td>";
        echo "<tr>";
        $fecha = $fechas[$i];
        $camas = 'SELECT cama.numero AS nombre, cama.id AS camaid FROM cama, habitacion WHERE habitacion.id = id_habitacion AND habitacion.id = '.$habid;
        $enviar_camas = mysql_query($camas, $enlace);
        //echo $camas;

        for($cams=1; $mostrar_camas = mysql_fetch_array($enviar_camas); $cams+=1){
            echo "<td>" . $mostrar_camas['nombre'] . "</td>";
            $fecha = $fechas[$i];
            $ocupacion2 = "SELECT cliente.id AS clienid, cliente.nombre AS nombre FROM cliente, evento, cama WHERE
            cliente.id = id_cliente AND cama.id = id_cama AND id_habitacion = " . $mostrar_habs['camaid'] . " AND checkin = \"" . $fecha . "\"";
            $enviar_ocupacion2 = mysql_query($ocupacion2, $enlace);

            for($cliens = 1; $mostrar_cliens = mysql_fetch_array($enviar_ocupacion); $cliens+=1){
                echo "<td>" . $mostrar_cliens['nombre'] . "</td>";
            }
            $i++;
        }
        echo "</tr>";
    }
    echo "</tr></table>";
}

The problem is in the first mysql_query

$habitaciones = "SELECT habitacion.id AS habid, habitacion.nombre AS habnom, tipo.num_cama AS cantidad FROM habitacion, tipo WHERE id_tipo = tipo.id";
                $enviar_sql = mysql_query($habitaciones, $enlace);

All the code depends on this query, at the browser returns

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ....

because the querys into if() doesn't have the value of the first query

Any idea? I don't understand why doesn't works

Thanks for all and sorry for my english

Upvotes: 0

Views: 150

Answers (3)

Joseadrian
Joseadrian

Reputation: 4374

error_reporting(E_ALL);
$habitaciones = 'SELECT h.id AS habid, h.nombre AS habnom, t.num_cama AS cantidad 
FROM habitacion h, tipo t
WHERE h.id_tipo = t.id'; // Less query? jajaa
$enviar_sql = mysql_query($habitaciones, $enlace);

And try with:

while ($mostar_habs = mysql_fetch_assoc($result)) {...

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838266

Try this:

$enviar_sql=mysql_query($habitaciones, $enlace) or trigger_error(mysql_error());

It will show you the error, allowing you to debug it.

Upvotes: 4

ThiefMaster
ThiefMaster

Reputation: 318518

Replace $enviar_sql = mysql_query($habitaciones, $enlace); with this:

$enviar_sql = mysql_query($habitaciones, $enlace) or die(mysql_error());

While this is an awful way to handle errors and should not be used except to fix whatever issue there is with your query it's a good way to quickly find out what's going wrong.

Upvotes: 3

Related Questions