Search filtering error (PHP)

Asked

Viewed 299 times

-1

I have two inputs date that is to take the date you receive from them and do a search in db to show all products purchased during this date, but is giving 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 html of inputs:

    <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 of how to get the data and trying to pull on 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);
}

Someone knows why it’s not working and how to fix it? No db o DATEV ta como date

3 answers

2


The message says Call to undefined function mysqli_result in [...]filtro.php:15. That is the function mysqli_result there is no.

This happened because there is not even the function mysql_result. In fact the return of function mysqli_query is a object of the kind mysqli_result (only in cases of consultation SELECT, SHOW, EXPLAIN and DESCRIBE). To fix the problem, just 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 facilitates connection to the database, have one I wrote, but feel free to search other:

It may be that she is a little outdated, because I stopped using Mysqli and started using PDO (Connectionpdo)

  • My version is 7.1.9 so that’s right

  • @Maria the function has been removed in version 7. See the response again with more attention in the line quoting such version.

  • But is that I did this function on another file and it works normally, tends? do not give any error

  • The Connection string is correct?

  • Hmmm, you’re using the mysqli_query? Can enter connection code (don’t forget to remove IP, user and password before posting), @Maria?

  • @Kaduamaral, yes, I use mysqli_query. I updated the question with the connection code

  • @Maria updated response. See if you can solve it now.

  • @Kaduamaral keeps making this mistake in this if Parse error: syntax error, unexpected 'if' (T_IF) in C:\xampp\htdocs\ComFiltro\config\filtro.php on line 16

  • Hi @Maria, it’s just ; I forgot, I put it there. Always when an error occurs, remember to check the error line and the previous line and see if the syntax is correct.

Show 4 more comments

1

Mysqli library does not have the function mysqli_result. In fact, she’s a class which is returned by the method mysqli_query or by the method 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 falling into the error of transcribing a script using functions mysql_* to the library mysqli. They look alike, but they’re not 1:1.

What you can do is use the immediate return of mysqli_query, that returns an object mysqli_result, and iterate on the result using some function fetch, just like the 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
}

-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 don’t know if you want to work with cookies.

If I had to choose, I probably wouldn’t use $_REQUEST and choose $_GET or $_POST, depending on what I intended my app to do (i.e., one or the other, but not both).

That said, you must use:

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

Anyway, there won’t be much difference compared to the performances: 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);
  }
}

 ?>
  • continues giving error Fatal error: Uncaught Error: Call to Undefined Function mysqli_result() in C: xampp htdocs Comfilter config filter.php:15 Stack trace: #0 C: xampp htdocs Comfilter home.php(74): include() #1 {main} thrown in C: xampp htdocs Comfilter config filter.php on line 15

Browser other questions tagged

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