mySQL query multiple - returns error mysql_fetch_array

前端 未结 3 1737
渐次进展
渐次进展 2021-01-28 10:34

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

quartos:

  • id_quarto.
  • tipo_quarto
相关标签:
3条回答
  • 2021-01-28 11:05

    Compare strings with LIKE (if they aren't the index)

    $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'] ."'";
    

    The while between closing ?> tags can to be done different (it's clearer IMHO):

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

    and

    <?php
        endwhile;
    

    and yeah, of course you'll have to do the actual mysql_query, like the others pointed out!

    0 讨论(0)
  • 2021-01-28 11:12
    1. You need to call mysql_query to get the resource set. See http://php.net/manual/en/function.mysql-fetch-array.php for an example.
    2. Should escape the $POST variables
    0 讨论(0)
  • 2021-01-28 11:27

    You forget about mysql_query, change:

    // 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)) {
    

    to:

    // 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)) {
    

    Added: Prevent SQL injection using mysql_real_escape_string on each parameter from user.

    0 讨论(0)
提交回复
热议问题