2011-03-04 50 views
0

我有这段代码,但第一个查询不运行(在phpmyadmin工作!),我试图在2台服务器上运行代码(可能是php/mysql的配置),但结果是一样的。在PHP中执行SQL查询时出现问题

$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>"; 
} 

的问题是在第一的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); 

所有的代码都依赖于这个查询,在浏览器返回

警告:mysql_fetch_array()预计参数1是资源,布尔值在...

因为查询成if( )没有第一个查询的值

有什么想法吗?我不明白为什么不工作

感谢所有和对不起我的英语

+5

为什么人们仍然使用MySQL的功能没有任何包装/正确的错误处理? – ThiefMaster 2011-03-04 23:39:53

+0

一个小小的猜测,你是否试图在where子句中提到表名?尝试WHERE tablename.id_tipo = tipo.id – sled 2011-03-04 23:55:39

回答

3

这种替换$enviar_sql = mysql_query($habitaciones, $enlace);

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

虽然这是一种处理错误的可怕方式和应除了解决您的查询中存在的任何问题之外,不要使用它,这是快速找出问题的一个好方法。

+0

感谢您的答案,但mysql_error()不会返回任何东西 – Javierh 2011-03-04 23:49:55

4

试试这个:

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

它会告诉你的错误,让您调试。

+1

+1因为不使用'die()'废话 – ThiefMaster 2011-03-04 23:44:25

+0

与@ThiefMaster相同,mysql_error()不返回什么:( – Javierh 2011-03-04 23:52:12

+0

你试图在phpmyadmin或mysql控制台中运行查询?如果查询很好,也许连接有问题? – sled 2011-03-04 23:53:32

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); 

并尝试:

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

Seguro $ enlace = mysql_connect('localhost','usuario','password');'estámal o'$ enviar_sql = mysql_query($ habitaciones)'; – Joseadrian 2011-03-05 02:15:28

+0

$ enlace que es el mysql_connect funciona perfectamente(otras partes de la misma web funcionan。El mysql_querydeberíafuncionar ya que la consulta en phpmyadmin funciona y muestra los da tos correctos – Javierh 2011-03-05 13:13:51

+0

唯一的迹象表明,PHP retuns是因为$ mostrar_habs ['habid'];和$ mostrar_habs ['habnom'];是未定义的变量:S – Javierh 2011-03-05 13:18:59