mySQL查询多次 - 返回错误mysql_fetch_array

时间:2012-01-11 10:44:44

标签: mysql

我有2个数据库表(对于预订系统),具有以下结构:

房间:

  • id_quarto
  • tipo_quarto
  • vista_quarto

饭店预订:

  • id_reserva
  • n_cliente
  • id_quarto
  • check_in
  • check_out

我希望查询返回可用的四分之一(房间)(其中包含字段id_quarto / tipo_quarto / vista_quarto),这些字段尚未在预留(预订)上预订,因此我编写以下查询(同时从以前的信息中选取信息)形式):

注意:此时我不考虑check_in和check_out日期因素......这只是一个测试,因此我会添加条件来检查它,但如果有人对这些条件有一些想法,我会感激。 :d

// Connect to database server
mysql_connect("localhost", "root") or die (mysql_error ());
// Select database
mysql_select_db("teste") or die(mysql_error());
// Get data from the database

$strSQL = "SELECT id_quarto,tipo_quarto,vista_quarto ".
          " FROM quartos,reservas ".
          " WHERE quartos.id_quarto!=reservas.id_quarto ".
          " AND quartos.tipo_quarto='". $_POST['tipo_quarto'] ."' ".
          " AND quartos.vista_quarto='". $_POST['vista_quarto'] ."'";

// Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {

?>
<table border="1">
    <tr align="left">
    <td width="75"><?php echo $row['id_quarto']; ?></td>
    <td width="75"><?php echo $row['vista_quarto']; ?></td>
    <td width="75"><?php echo $row['tipo_quarto']; ?></td></tr>
  </table>
 <?php 
 }

   // Close the database connection
//  mysql_close(); ?>

但是当我这样做时,它会在第X行返回一个错误,这是我循环记录集时说“mysql_fetch_array()希望参数1是资源,布尔”的行。

为什么这是我能做些什么来阻止它呢?我该如何编写正确的代码?

此外,我希望结果显示为选择(列表/菜单)表单项,以便用户只能选择有效的结果。知道如何将记录集的结果与此功能结合起来吗?

3 个答案:

答案 0 :(得分:2)

您忘了mysql_query,更改:

// Select database
mysql_select_db("teste") or die(mysql_error());

// Get data from the database

$strSQL = "SELECT id_quarto,tipo_quarto,vista_quarto FROM quartos,reservas WHERE quartos.id_quarto!=reservas.id_quarto AND quartos.tipo_quarto='". $_POST['tipo_quarto'] ."' AND quartos.vista_quarto='". $_POST['vista_quarto'] ."'";

// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {

为:

// Select database
mysql_select_db("teste") or die(mysql_error());

// Get data from the database

$strSQL = "SELECT q.id_quarto, q.tipo_quarto, q.vista_quarto ".
          " FROM quartos q, reservas r".
          " WHERE q.id_quarto != r.id_quarto ".
          " AND q.tipo_quarto = '". mysql_real_escape_string($_POST['tipo_quarto']) ."' ".
          " AND q.vista_quarto = '". mysql_real_escape_string($_POST['vista_quarto']) ."'";

$rs = mysql_query($strSQL);

// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {

已添加:使用mysql_real_escape_string阻止来自用户的每个参数的SQL注入。

答案 1 :(得分:0)

  1. 您需要致电mysql_query以获取资源集。有关示例,请参阅http://php.net/manual/en/function.mysql-fetch-array.php
  2. 应该转义$ POST变量

答案 2 :(得分:-1)

将字符串与LIKE进行比较(如果它们不是索引)

$strSQL = "SELECT id_quarto,tipo_quarto,vista_quarto FROM quartos,reservas WHERE quartos.id_quarto!=reservas.id_quarto AND quartos.tipo_quarto LIKE '". $_POST['tipo_quarto'] ."' AND quartos.vista_quarto LIKE '". $_POST['vista_quarto'] ."'";

关闭while代码之间的?>可以做得不同(恕我直言):

    while($row = mysql_fetch_array($rs)) :
?>

<?php
    endwhile;

是的,当然你必须像其他人指出的那样做mysql_query