0

I am developing a system that searches for products on a data base by description, everthing worked fine, I had no problem during my sentences, the problem came today when I tried searching using an Emoji as input, it did only not returned an empty array as it happens when the description doesnt match anything but it crashed my code and PHP said "Call to a member function fetch_assoc() on bool" well I then used the mysqli->errno to see what the problem was and I found out it was returning error number 2014. Here's part of my code

<?php
function select($connection, $query, $dataType, $data) {
        $preparedQuery = $connection->prepare($query);
        if ($preparedQuery === false) {
            //You may ignore this since it's sending messages in another language to the user
            //All of my responses are sent in JSON format to the client
            $error = $connection->error;
            return '{"exito":"no","motivo":"Error en la consulta: ' . $error . '","consulta":"' . $sentencia . '"}';
        }
        if (count($data) > 0) {
            $preparedQuery ->bind_param($dataType, ...$data);
        }
        $preparedQuery->execute();

        $i = 0;
        $results= $preparedQuery->get_result();
        $registros = [];
        if($results=== false){
            //This is where I found out I was getting the 2014 error
            echo $connection->errno;
        }
        while ($temporal = $results->fetch_assoc()) {
            $registros[$i] = $temporal;
            $i++;
        }
        //There's more code below
}

function connect() {
    $server = "localhost";
    $user = "root";
    $password = "";
    $db = "electrodomesticos";
    $connection= new mysqli($server, $user, $password, $db) or die("Error en la conexion a Mysql");
    $connection->set_charset("utf8");
    return $connection;
}

$connection = connect();
$description = $_POST["description"];
$query = "select * from products where descripcion like ?";
echo select($connection, $query, "s", ["%" .$description . "%"]);

That is not exactly how I've written my code, but in essence it is something like that, Why would my code crash and return error 2014?

I would really appreciate if somebody help mewith this, thank you in advance

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Noé
  • 149
  • 8
  • Where is `$sentencia_select` defined? It would be better if you posted your *actual* code up to the point where you get the error... – Nick Mar 13 '21 at 03:42
  • @Nick I'm sorry I corrected the code, it is just that it may be a lot of code and I changed the variable names to make them easier to understand. I hope it helps and thanks for checking. – Noé Mar 13 '21 at 03:48
  • Try using `mysqli_error` to get the actual message – Tangentially Perpendicular Mar 13 '21 at 04:26
  • I am closing this question as a duplicate for the error message you get. Most likely it is caused by using a stored procedure. The problem with emoji is solved by using utf8mb4 charset. Instead of checking errno you must properly configure [mysqli connection](https://phpdelusions.net/mysqli/mysqli_connect) and [PHP error reporting](https://phpdelusions.net/articles/error_reporting) – Your Common Sense Mar 13 '21 at 05:14
  • Oh, and there is no PDO in this code – Your Common Sense Mar 13 '21 at 05:15
  • The idea for select function is quite smart, though it could be written [much simpler](https://phpdelusions.net/mysqli/simple). – Your Common Sense Mar 13 '21 at 05:31
  • Yeah the problem was solved by changing utf8 to utf8mb4, I don't know why but it gave that error (2014), now that I've changed the charset it is not throwing that error anymore and it works just fine. thanks @YourCommonSense Oh by the way, I tried to catch most of errors in my select function so it doesnt break my code when I have a sintax error in my queries it just displays a custom message instead. Thank you so much... – Noé Mar 13 '21 at 14:20
  • And you did **everything** wrong. A select function **it is not where you should catch errors**, let alone *display* anything. SQL is not the only source of errors. It is not a "custom message" you need as a programmer. Please read [basic principles of programming](https://phpdelusions.net/basic_principles_of_web_programming#error_reporting) – Your Common Sense Mar 13 '21 at 14:44

0 Answers0