get_result () not recognized

1

Good day!

I'm doing an autocomplete for an input in ajax through php and mysql, the tutorial here if you want to see it

link

the php adapting it to my database

<?php
require('conectar.php');
    $keyword = strval($_POST['query']);
    $search_param = "{$keyword}%";
    $sql = $conexion->prepare("SELECT distinct nombre_cliente FROM cuentas WHERE nombre_cliente LIKE '%".$_POST["query"]."%' ORDER BY nombre_cliente ASC");
    $sql->bind_param("s",$search_param);
    $sql->execute();
    $result = $sql->get_result();
    if ($result->num_rows > 0)
  {
        while($row = $result->fetch_assoc()) {
        $resultado_nombre_cliente[] = $row["nombre_cliente"];
        }
        echo json_encode($resultado_nombre_cliente);
    }
    $conexion->close();
?>
  

Uncaught Error: Call to undefined method mysqli_stmt :: get_result () in   /home2/gabriel/perezrodriguezabogados.com/sistema/PHP/autocompletar.php:8

I've read that you have to install something from a controller but I'm using cpanel in a common hosting account, in this case hostgator and I do not know if you can do that

Is there another way to write it and that I do what is required?

I leave the script in case, although I do not think it's necessary

$(document).ready(function () {
        $('#input_nombre_cliente').typeahead({
            source: function (query, result)
            {
              $.ajax({
                url: "../PHP/autocompletar.php",
                data: 'query=' + query,
                dataType: "json",
                type: "POST",
                success: function (data)
                {
                  result($.map(data, function (item) {
                    return item;
                  }));
                }
              });
            }
        });
    });
    
asked by Gabriel Uribe Gomez 27.03.2018 в 20:27
source

1 answer

1

The problem

The PHP Manual says the following about get_result :

  

MySQL Native Driver Only

     

Available only with mysqlnd .

That is, if you do not have the mysqlnd drive installed, the get_result function will not be available, as indeed it is happening, so the message: Call to undefined method mysqli_stmt::get_result()...

The solution

Since you are on a shared hosting, you may have difficulty installing the mysqlnd driver. You could explore this possibility anyway, by contacting the technical service of your hosting, if you are interested in more features of this driver .

Now, if you're only interested in using get_result , you can choose a simpler solution: Implement your own function get_result . It is a handmade function that does the same thing as get_result .

This feature was proposed by @aldanux in response to the question: How to obtain an associative array using queries prepared with mysqli? . It is an interesting solution, to avoid writing a code that may not work in some environments that do not have mysqlnd installed:

function get_result( $stmt ) {
    $arrResult = array();
    $stmt->store_result();
    for ( $i = 0; $i < $stmt->num_rows; $i++ ) {
        $metadata = $stmt->result_metadata();
        $arrParams = array();
        while ( $field = $metadata->fetch_field() ) {
            $arrParams[] = &$arrResult[ $i ][ $field->name ];
        }
        call_user_func_array( array( $stmt, 'bind_result' ), $arrParams );
        $stmt->fetch();
    }
    return $arrResult;
}

You can use this function:

  • be in the same script where you recover the data
  • be putting it in a Clase utilitarian%, where you put all your utilitarian functions. This would be the most recommended way, especially in large applications. That way, you have the function in a single site and when you need it you create an instance of said Clase utilitarian%.

How to use

It would be very simple. You pass to the function the $stmt and it returns an associative array with the results.

Taking your code, it would be something like this:

$result = get_result($sql);

To check if there were no results, you will not need to invoke num_rows , you can do it simply like this:

if ($result) {
//hubo resultados. Los leemos...
    foreach ($result as $row){
        $resultado_nombre_cliente[] = $row["nombre_cliente"];
    }
echo json_encode($resultado_nombre_cliente);    
}else{
echo "No se encontraron datos";
}

P. D .: If there is any doubt, here is a complete example , where this function is implemented.

    
answered by 28.03.2018 / 01:17
source