-1

Possible Duplicate:
mySQL query multiple - returns error mysql_fetch_array

I have 2 database tables (for a booking system) with the following structures:

quartos:

id_quarto.
tipo_quarto.
vista_quarto.

reservas:

id_reserva.
n_cliente.
id_quarto.
check_in.
check_out.

I want the query to return the quartos (rooms) available (with the fields id_quarto / tipo_quarto / vista_quarto from it) which arent already being booked on reservas (reservations) so i write the following query (also picking information from a previous form):

NOTE: At this time i am not considering the check_in and check_out dates factor... this is only a test and therefore i will add the conditions to check it too, but if anyone has some ideas for those conditions i would be grateful. :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='". mysql_real_escape_string($_POST['tipo_quarto']) ."' ".
              " AND quartos.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)) {

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

But when I do this it returns an error on Line X, which is the line when i loop the recordset saying that "mysql_fetch_array() expects parameter 1 to be resource, boolean".

Why is this and what can i do to prevent it? how do i write the correct code?

Also, i wanted the results to be featured as a Select (List/Menu) form item so that user the could only choose the valid results. Any idea how to incorporate the results from the recordset with this feature?

6
  • 1
    You are still not using mysql_query(), are you? Commented Jan 11, 2012 at 11:43
  • Is the "SELECT id_quarto" in your query supposed to be referring to quartos.id_quarto or reservas.id_quarto? And does that query even work without specifying that?! Where are you executing the query? Commented Jan 11, 2012 at 11:45
  • Duplication of stackoverflow.com/questions/8817940/…. Commented Jan 11, 2012 at 11:45
  • @MarkByers it is reffering to quartos.id_quarto Commented Jan 11, 2012 at 11:49
  • You're still not checking the result of the query. Commented Jan 11, 2012 at 11:53

2 Answers 2

1

You missed

$rs=mysql_query($strSQL)

Ofcourse immediately before

// Loop the recordset $rs
2
  • $rs = mysql_query($strSQL); is included. sorry. still doesnt work. Commented Jan 11, 2012 at 11:48
  • Next step: do "echo $strSQL;" before "$rs = mysql_query($strSQL);", copy the SQL to MySQL command line client and post result. Commented Jan 11, 2012 at 11:50
0

Strictly you should be saying:

if ($rs=mysql_query($strSQL)) {

// Proceed with while loop

} else echo mysql_error();

The query might be raising an error???

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.