Error in select using mysqli_query

Asked

Viewed 22,753 times

5

I made a php to list the rooms, but I’m having difficulty to list them, if anyone can help me, I would appreciate

<?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>

Mistakes:

Warning: mysqli_query() expects at least 2 Parameters, 1 Given in F: XAMPP htdocs Pap2 quarto.php on line 67

Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, null Given in F: XAMPP htdocs Pap2 quarto.php on line 69

  • Please place your code here, removing sensitive information that could compromise your system. Only the code that contains the problem and relevant parts. So we can help you better.

  • already this, can help pfv

  • What’s the problem? string values need simple quotes in sql.

  • 1

    To put the code, next time, select all your code and press the button with the "{}" icon. Or, put four spaces before each line. This is important especially with codes starting <?, because that’s what the text formatting engine interprets as the beginning of an invisible text.

3 answers

3

The error reported:

Warning: mysqli_query() expects at least 2 Parameters, 1 Given in F: XAMPP htdocs Pap2 quarto.php on line 67

It’s because we’re missing the link Mysqli by parameter, previously missing in Mysql functions:

Correct by passing the variable $link 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 quarto.php on line 69

It is because the parameter of the type of fetch:

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

The options of fetch sane:

  1. MYSQLI_NUM (back in a numeric array $row[0])
  2. MYSQLI_ASSOC (back in an associative array $row['descricao'])
  3. MYSQLI_BOTH (behind the previous two modes)

I also recommend using Object-Oriented Mysqli, 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’s a problem in the cloister WHERE where a string must be passed inside single quotes:

SELECT descricao FROM quartos WHERE nome_quarto='Quarto Ibiza'

This is the PHP code:

$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

  • Give me the following errors :( Warning: mysqli_query() expects at least 2 Parameters, 1 Given in F: XAMPP htdocs Pap2 quarto.php on line 67 Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, null Given in F: XAMPP htdocs Pap2 quarto.php on line 69

3

Warning: mysqli_query() expects at least 2 Parameters, 1 Given in F: XAMPP htdocs Pap2 rooms.php online 67

This error says that the function expects two arguments and only one has been passed, so it is required 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 simple quotes 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 rooms.php on line 69

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

  • VERY BUSY, PROBLEM SOLVED! THANK YOU VERY MUCH

2

I believe that’s 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.