I’m getting two errors when I try to search by name in a product table

Asked

Viewed 64 times

-1

I’m scheduling a product management page, and I want to include a search engine to give users a search engine that can save time while they’re looking for the desired product, but after following a tutorial for the same I am encountering two errors:

  • Warning: mysqli_query() expects at least 2 Parameters, 1 Given in C: xampp htdocs administracao produtos index.php on line 82

  • Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, null Given in C: xampp htdocs administracao produtos index.php on line 84

The PHP code is as follows::

<!-- Pesquisar PHP ------------->
<?php
if (isset($_POST['submit'])) {
    if (isset($_GET['go'])) {
        if (preg_match("/[A-Z  | a-z]+/", $_POST['name'])) {

            $name = $_POST['name'];
            //connect  to the database 
            $db = mysqli_connect("localhost", "root", "") or die('I cannot connect  to the database because: ' . mysqli_error());
            //-select  the database to use


            mysqli_select_db($db, "pap_database") or die("cannot select DB");
            $sql = "SELECT idprodutos, nome, ano_atual FROM produtos WHERE nome LIKE '%" . $name . "%' OR ano_atual LIKE '%" . $name . "%'";
            //-run  the query against the mysql query function 
            $result = mysqli_query($sql);
            //-create  while loop and loop through result set 
            while ($row = mysqli_fetch_array($result)) {
                $name = $row['nome'];
                $ano = $row['ano_atual'];
                $ID = $row['idprodutos'];
                //-display the result of the array 
                echo "<ul>\n";
                echo "<li>" . "<a  href=\"index.php?idprodutos=$ID\">" . $name . " " . $ano . "</a></li>\n";
                echo "</ul>";
            }
        } else {
            echo "<p>Please enter a search query</p>";
        }
    }
}
?>
<!------------------------------>

HTML code:

<form action="index.php?go" method="post" id="searchform">
    <input type="text" name="name" placeholder="Pesquisa etc" />
    <input type="submit" name="submit" value="Search" />

    <?php
    //end of search form script
    if (isset($_GET['by'])) {
        $letter = $_GET['by'];
        //connect  to the database
        $db = mysqli_connect("localhost", "root", "") or die('I cannot connect to the database  because: ' . mysqli_error());
        //-select  the database to use
        $mydb = mysqli_select_db("pap_database");
        //-query  the database table
        $sql = "SELECT  idprodutos, nome, ano_atual FROM produtos WHERE nome LIKE '%" . $letter . "%' OR ano_atual LIKE '%" . $letter . "%'";
        //-run  the query against the mysql query function
        $result = mysqli_query($sql);
        //-count  results
        $numrows = mysqli_num_rows($result);
        echo "<p>" . $numrows . " results found for " . $letter . "</p>";
        //-create  while loop and loop through result set
        while ($row = mysqli_fetch_array($result)) {
            $name = $row['nome'];
            $ano = $row['ano_atual'];
            $ID = $row['idprodutos'];
            //-display  the result of the array
            echo "<ul>\n";
            echo "<li>" . "<a  href=\"search.php?id=$ID\">" . $name . " " . $ano . "</a></li>\n";
            echo "</ul>";
        }
    }
    //end of our letter search script
    if (isset($_GET['idprodutos'])) {
        $contactid = $_GET['idprodutos'];
        //connect  to the database
        $db = mysqli_connect("localhost", "root", "") or die('I cannot connect to the database  because: ' . mysql_error());
        //-select  the database to use
        $mydb = mysqli_select_db("pap_database");
        //-query  the database table
        $sql = "SELECT  * FROM produtos WHERE idprodutos=" . $contactid;
        //-run  the query against the mysql query function
        $result = mysqli_query($sql);
        //-create  while loop and loop through result set
        while ($row = mysqli_fetch_array($result)) {
            $name = $row['nome'];
            $ano = $row['ano_atual'];
            $ID = $row['idprodutos'];
            //  $PhoneNumber=$row['PhoneNumber'];
            //  $Email=$row['Email'];
            //-display  the result of the array
            echo "<ul>\n";
            echo "<li>" . $name . " " . $ano . "</li>\n";
            //echo  "<li>" . $PhoneNumber . "</li>\n";
            //echo  "<li>" . "<a href=mailto:" . $Email .  ">" . $Email . "</a></li>\n";
            echo "</ul>";
        }
    }
    ?>
</form>
  • 1

    Possible duplicate of Error in select using mysqli_query

  • @8bit the situation dealt with in this topic is different from my situation.

  • yes, the situation is probably different but the mistakes are the same, you checked the errors that are in the question ? And the Answers ?

  • So what should I do to fix this?

2 answers

1

As can be seen in the PHP documentation mysqli_query receives two input parameters, where the first is the successful database connection and the second is the SQL to be searched.

https://www.php.net/manual/en/mysqli.query.php

I believe the solution to your case would look like this:

$result = mysqli_query($db, $sql);

  • I have already consulted the document, but I still do not know what I have wrong or what I can do to solve.

0

Good afternoon! First of all be careful with the names you give! In your PHP code there are two or more $sql repeated.. may be giving error there. Anyway you also have a syntax error...

$result = mysqli_query($sql,**FALTA AQUI UM CONNECT PARA A BD**);
            while ($row = mysqli_fetch_array($result)) {

and try to change the mysqli_fetch_array for mysqli_fetch_assoc because if you want to execute a printf or a echo is more noticeable.

  • Warning: mysqli_query() expects Parameter 1 to be mysqli, string Given in C: xampp htdocs administracao produtos index.php on line 82 Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, null Given in C: xampp htdocs administracao produtos index.php on line 84

Browser other questions tagged

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