Error with fetch_assoc MYSQLi

1

I come to you since I am stuck in an object-oriented MySQLi function.

I try to get the ID (A_i) of the corresponding tokens of the users, in a table called Tokens. The problem is that it does not give errors and also returns an empty result.

I clarify that the token I use to test the code is exactly the same as it is in the table, since I basically copy and paste the same, so it is not a problem of non-existence of the same.

I would like to know what I am doing wrong, because I have been stuck for exactly 1 week. Thanks in advance.

I leave the code:

modules / config.php

<?php

    $host = "localhost";
    $db_user = "root";
    $db_pass = "123456";
    $database = "database";

    $db = new mysqli($host, $db_user, $db_pass, $database);

?>

classes / token.php

<?php

    class Token
    {
        function usuarioToken($token)
        {
            include("../modules/config.php");

            // Seleccion de datos segun token
            $sql = "SELECT * FROM 'tokens' WHERE 'token'='".$token."'";
            $db->query($sql);
            $buffer = [];
            while ($data = $db->fetch_assoc) {
                $buffer[] = $data["id"];
            }
            return $buffer;
        }
    }

?>

verify / index.php

<?php

    include("../classes/token.php");

    $token = new Token();

    print_r( $token->usuarioToken($_GET["t"]) );

?>

Result obtained ...

Array ( )
    
asked by Skollprog 25.10.2017 в 21:21
source

1 answer

2

I already saw your error.

You are acting on $db in the fetch, and you must act on the returned results.

class Token
{
    function usuarioToken($token)
    {
        include("../modules/config.php");

        // Seleccion de datos segun token
        $sql = "SELECT * FROM 'tokens' WHERE 'token'='".$token."'";
        $resultado=$db->query($sql); //Resultado de la consulta
        $buffer = [];
        while ($data = $resultado->fetch_assoc()) { //con ()
            $buffer[] = $data["id"];
        }
        return $buffer;
    }
}
  

Note:

     

It is recommended to use queries prepared to give security to your   BD.

I will write here the code using prepared queries and evaluating possible errors:

class Token
{
    function usuarioToken($token)
    {
        include("../modules/config.php");

        if ($db)
        {

            // Seleccion de datos segun token
            $sql = "SELECT id FROM 'tokens' WHERE 'token'=?";  //Parece que sólo quieres el id

            $stmt=$db->prepare($sql);

            if ($stmt){
            $stmt->bind_param("s", $token);
            $stmt->execute();
            $stmt->store_result();
            $resultado=$db->query($sql); //Resultado de la consulta
            $buffer = [];
            while ($data = $resultado->fetch_assoc()) { //con ()
                $buffer[] = $data["id"];
            }

            return $buffer;
            $stmt->close();

            }else{

                echo "Error preparando la consulta";

            }

        //Cerrar $db si es preciso

        }else{

            echo "Error en la conexión";

        }


    }
}
    
answered by 25.10.2017 / 21:35
source