0

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

2
  • 5
    Why do people still use mysql functions without any wrapping/proper error handling? Commented Mar 4, 2011 at 23:39
  • a tiny guess, did you try to mention the table name in the where clause? Try WHERE tablename.id_tipo = tipo.id Commented Mar 4, 2011 at 23:55

3 Answers 3

4

Try this:

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

The same that @ThiefMaster, mysql_error() doesn't return anything :(
have you tried to run the query in phpmyadmin or mysql console? If the query is fine, maybe something is wrong with the connection?
The query in phpmyadmin runs ok and the connection too, I have another php file with the same connection to the same database and works fine.
3

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.

1 Comment

Thanks for the answer but mysql_error() doesn't return anything
0
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)) {...

5 Comments

Seguro $enlace = mysql_connect('localhost', 'usuario', 'password'); está mal o $enviar_sql = mysql_query($habitaciones);
$enlace que es el mysql_connect funciona perfectamente (otras partes de la misma web funcionan. El mysql_query debería funcionar ya que la consulta en phpmyadmin funciona y muestra los datos correctos
The only notices that php retuns are because the $mostrar_habs['habid']; and $mostrar_habs['habnom']; are undefined variable :S
Ya vi el problema... has puesto while(**mostar_habs**) y dentro de la lógica usas **mostrar** :) Jeje
@Joseadrian eres un genio, funciona! Te debo una cerveza! // @Joseadrian, you are a genius, it works! I owe a beer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.