No select error using mysqli_query

5

I've done a php to list the rooms, but I'm having difficulty listing them, if anyone can help me, I would appreciate it.

<?php
$link = mysqli_connect("localhost","root","","hotel");
$result = mysqli_query("SELECT descricao FROM quartos WHERE nome_quarto=Quarto Ibiza");
$row = mysqli_fetch_array($result);
?>
<table class="table table-bordered">
    <tr>
        <th class="tg-031e" colspan="3"></th>
    </tr>
    <tr>
        <td class="tg-031e"><img src="fotos/ibiza.jpg" height="300px"></td>
        <td class="tg-031e">
            <b><font size="5px" color="black">Descrição:</font></b>
            <p><font color="black" size="3px">
      <!-- Mostrar os dados que fui buscar ao select -->
            <?php
            echo $row['descricao'];
            ?>
            </p></font>
            <button type="submit" id="button1id" name="button1id"                 style="border:3px double black;" class="btn btn-primary" >Reservar</button> 
</td>
</tr>
</table>

Errors:

  

Warning: mysqli_query () expects at least 2 parameters, 1 given in F: \ XAMPP \ htdocs \ Pap2 \ bedrooms.php on line 67

     

Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result, null given in F: \ XAMPP \ htdocs \ Pap2 \ bedrooms.php on line 69

    
asked by anonymous 08.06.2015 / 21:53

3 answers

3
  

Warning: mysqli_query () expects at least 2 parameters, 1 given in   F: \ XAMPP \ htdocs \ Pap2 \ bedrooms.php on line 67

This error says that the function expects two arguments and only one is passed, so it is It is necessary to pass two arguments to the function [mysqli_query()][1] the first the connection and the second the query, it is necessary to pass the values in the query between single quotation marks when these values are of type string.

$result = mysqli_query($link, "SELECT descricao FROM quartos WHERE nome_quarto='Quarto Ibiza'");
  

Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result,   null given in F: \ XAMPP \ htdocs \ Pap2 \ bedrooms.php on line 69

Normally this error indicates that your query has a syntax error (sql state 1054 mysql)

    
08.06.2015 / 22:01
3

The error reported:

  

Warning: mysqli_query () expects at least 2 parameters, 1 given in F: \ XAMPP \ htdocs \ Pap2 \ bedrooms.php on line 67

This is because you have to pass the link of MySQLi per parameter, but nonexistent in the MySQL functions:

Correct by passing the $link variable as follows:

$link = mysqli_connect("localhost","root","","hotel");
$result = mysqli_query($link, "SELECT descricao FROM quartos WHERE nome_quarto='Quarto Ibiza'");

And the second:

  

Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result, null given in F: \ XAMPP \ htdocs \ Pap2 \ bedrooms.php on line 69

It's because the fetch type parameter was missing:

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

fetch options are:

  • MYSQLI_NUM (back in numeric array $row[0] )
  • MYSQLI_ASSOC (back in associative array $row['descricao'] )
  • MYSQLI_BOTH (behind the previous two modes)
  • I also recommend using MySQL Object Oriented, like this:

    $link = new mysqli("localhost","root","","hotel");
    $result = $link->query("SELECT descricao FROM quartos WHERE nome_quarto='Quarto Ibiza'");
    $row = $result->fetch_array(MYSQLI_ASSOC);
    

    Another error in your sql query:

    SELECT descricao FROM quartos WHERE nome_quarto=Quarto Ibiza
    

    There is a problem with WHERE where a string must be passed inside single quotation marks:

    SELECT descricao FROM quartos WHERE nome_quarto='Quarto Ibiza'
    

    The PHP code looks like this:

    $result = mysqli_query($link, "SELECT descricao FROM quartos WHERE nome_quarto='Quarto Ibiza'");
    

    I have a connection class that helps a lot when managing the connection: ConnectionMSi

        
    08.06.2015 / 21:59
    2

    I believe this is what you need:

    <table class="table table-bordered">
        <tr>
            <th class="tg-031e" colspan="3"></th>
        </tr>
        <tr>
            <td class="tg-031e"><img src="fotos/ibiza.jpg" height="300px"></td>
            <td class="tg-031e">
                <b><font size="5px" color="black">Descrição:</font></b>
                <p><font color="black" size="3px">
                <?php
                    $conn = mysqli_connect("localhost","root","","hotel");
                    $sql = "SELECT descricao FROM quartos WHERE nome_quarto='Quarto Ibiza'"
                    $result = mysqli_query($conn, $sql);
    
                    while($row = mysqli_fetch_assoc($result)) {
                        echo $row['descricao'];
                    }
    
                    mysqli_close($conn);
                ?>
                </p></font>
                <button type="submit" id="button1id" name="button1id" style="border:3px double black;" class="btn btn-primary" >Reservar</button> 
            </td>
        </tr>
    </table>
    
        
    08.06.2015 / 22:02