Error in search filtering (PHP)

-1

I have two inputs date which is to get the date it receives from them and do a search on db to show all the products purchased during that date, but it gives the following error:

Fatal error: Uncaught Error: Call to undefined function mysqli_result() in C:\xampp\htdocs\ComFiltro\config\filtro.php:15 Stack trace: #0 C:\xampp\htdocs\ComFiltro\home.php(74): include() #1 {main} thrown in C:\xampp\htdocs\ComFiltro\config\filtro.php on line 15

Here is the input html:

    <div class="main">
        <h1 class="center port-1 page-header">Portal Guido</h1>
        <!-- -->
        <div class="jumbotron jumb-1">
          <h4 class="center">Informe a data desejada para a busca dos produtos</h4><br>
              <form class="form-inline text-center" method="">
                <div class="form-group">
                  <label for="datainicial">Data inicial</label>
                  <input type="date" class="form-control" id="inicio" name="inicio" placeholder="Data de inicio">
                </div>
                <!-- -->
                <div class="form-group">
                  <label for="datafinal">Data final</label>
                  <input type="date" class="form-control" id="fim" name="fim" placeholder="Data final">
                </div>
                <!-- -->
                <button type="submit" id="filtro" name="filtro" value="filtro" class="btn btn-primary">
                  Buscar
                </button>
                                <?php
                      include "config/filtro.php";
                     ?>
              </form>
        </div>
</div> <!--MAIN -->

Here's the code for how to get the data and trying to pull in db:

<?php

require_once "config/conexao.php";

if (isset($_REQUEST['filtro'])) {

    $inicio = $_REQUEST['inicio'];
    $fim = $_REQUEST['fim'];

    $query = "SELECT * from api_teste WHERE DATEV BETWEEN date('$inicio') AND date('$fim')";


    $querySelect = mysqli_query($conn,$query);

    if(mysqli_result($querySelect) == true){
      $mensagem = "<div class='alert alert-danger'>Há!</div>";
      printf ($mensagem);
    }else {
      $mensagem = "<div class='alert alert-danger'>Por favor coloque uma data válida!</div>";
      printf ($mensagem);
  }
}

 ?>

CONNECTION CODE:

<?php
$conn = new mysqli("localhost", "", "", "");

if ($conn->connect_error) {
    die("Falha ao conectar!, Motivo: " . $conn->connect_error);
}

Does anyone know why it's not working and how to fix it? No db or DATEV is set to date

    
asked by anonymous 15.02.2018 / 15:07

3 answers

2

The message says Call to undefined function mysqli_result in [...]filtro.php:15 . In other words, the mysqli_result function does not exist.

This occurred because there is not even the mysql_result function. In fact, the return of the mysqli_query function is a object of type mysqli_result (only in cases of SELECT, SHOW, EXPLAIN and DESCRIBE queries). To fix the problem, simply change your code to:

$querySelect = mysqli_query($conn,$query);
$rows = $querySelect->fetch_assoc();

if(count($rows) > 0){

For more details, read the documentation:

PS: I recommend using a library that eases the connection to the database, there is one that I have written, but feel free to search for another:

It may be a bit out of date, as I stopped using MySQLi and started using PDO ( ConnectionPDO ) / p>     

15.02.2018 / 15:29
1

The MySQLi library does not have the mysqli_result function. In fact, it is a class that is returned by the mysqli_query or by the mysqli_stmt::get_result (when using a prepared statement ).

Mysqli_query return value :

  

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query () will return a mysqli_result object . For other successful queries mysqli_query () will return TRUE.

Mysqli_stmt :: get_result :

  

Returns a resultset for successful SELECT queries , or FALSE for other DML queries or on failure. The mysqli_errno () function can be used to distinguish between the two types of failure.

What you are doing is to fall into the error of transcribing a script by using mysql_* functions for the mysqli library. They are similar, but not 1: 1.

What you can do is to use the immediate return of mysqli_query , which returns a mysqli_result object, and iterate over the result using some fetch function, such as mysqli_result::fetch_assoc .

Object-oriented version:

$querySelect = mysqli_query($conn,$query);
while($row = $querySelect->fetch_assoc())
{
    //iteração de cada resultado
}

Procedural version:

$querySelect = mysqli_query($conn,$query);
while($row = mysqli_fetch_assoc($querySelect))
{
    //iteração de cada resultado
}
    
16.02.2018 / 14:14
-1

$ _REQUEST, by default, contains the contents of $ _GET, $ _POST and $ _COOKIE.

But it's just a pattern, which depends on variables_order; and I do not know if you want to work with cookies.

If I had to choose, I probably would not use $ _REQUEST and would choose $ _GET or $ _POST, depending on what I wanted my application to do (ie, one or the other but not both).

That said, you should use:

  • $ _GET when someone is requesting data from your application and should use
  • $ _POST when someone is pushing (inserting or updating or deleting) data for your application.

Anyway, there will not be much difference in performance: the difference will be insignificant compared to what the rest of the script will do.

<?php

include "config/conexao.php";

if (isset($_GET['filtro'])) {

    $inicio = $_GET["inicio"];
    $fim =$_GET["fim"];

    $query = "SELECT * from api_teste WHERE DTAENTORC BETWEEN date('$inicio') AND date('$fim')";


    $querySelect = mysqli_query($conn,$query);

    if(mysqli_result($querySelect) == true){
      $mensagem = "<div class='alert alert-danger'>Há!</div>";
      printf ($mensagem);
    }else {
      $mensagem = "<div class='alert alert-danger'>Por favor coloque uma data válida!</div>";
      printf ($mensagem);
  }
}

 ?>
    
15.02.2018 / 15:27