Fatal error: Using $ this when not in object context in

0

I have this code:

function enlaces($X){
        $a = $this->db->query("SELECT * FROM enlaces WHERE codigo = '$X'");
        if($a->num_rows()>=1){
            return $a->fetch_array();
        }else{
            return false;
        }
    }


    function paneles(){
        $a = $this->db->query("SELECT * FROM paneles");

        if($a->num_rows()>=1){
            return $a->fetch_array();
        }else{
            return false;
        }
    }


    $paneles = paneles();

    if($paneles){
            //Inicio la lista Principal
            echo "<ul>";
        foreach($paneles as $item){
            //Inicio el Item de la Lista
            echo "<li>".$item['titulo'];
            $enlaces = enlaces($item['codigo']);
            //Valido si hay enlaces para el codigo
            if($enlaces){
                //Si Hay enlaces inicio la sublista del item
                echo "<ul>";
                //Recorro los enlaces retornados
                foreach($enlaces as $en){
                    //Agrego las URL
                    echo "<li>".$en['url']."</li>";
                }
                //Cierro la sub lista
                echo "</ul>";
            }else{
                //Si no hay URLs indico el mensaje
                echo "<li>Sin Enlaces para este panel</li>";
            }
            //Cierro el item principal
            echo "</li>";
        }
        //Cierro la lista Completa
        echo "</ul>";

    }else{
        //Si no hay paneles muestro el mensaje
        echo "No hay paneles que mostrar";
    }

With the following error: Fatal error: Using $ this when not in object context in

I understand that it is because This is not inside a class and here is where I get lost. What could be the class for the code to work well?

Additionally I am adding

db = mysqli_connect("localhost", "root", "roor", "flix");
function enlaces($X){
    $a = $this->db->query($db, "SELECT * FROM enlaces WHERE codigo = '$X'");

but I do not know if this is the right thing to do.

    
asked by Iván Camilo Gudiño Ochoa 29.03.2018 в 01:48
source

2 answers

0

You have two options:

  • Create a class that wraps the whole code

    O

  • Pass the variable db as a parameter to the functions

  • answered by 29.03.2018 в 02:15
    0

    If you want to handle everything object oriented, then you must create a class where you use $ this, because if it is not handled within a model, it will not work and it will call the variable $ this and then show you the error that has shared.

    I will use a connection with class so that I can then do the necessary tests:

    <?php
        class DriveDB {
            public $db_connect;
    
            function __construct($ip_host, $username, $password, $db) {
                if (!$this->db_connect = new mysqli($ip_host, $username, $password, $db))
                    echo "No hay conexión";
            }
    
            public function enlaces($X){
                $a = $this->db_connect->query("SELECT * FROM enlaces WHERE codigo = '".$X."';");
    
                if($a->num_rows > 0)
                    return $a->fetch_array(MYSQLI_ASSOC);
    
                return false;
            }
    
            public function paneles(){
                $a = $this->db_connect->query("SELECT * FROM paneles;");
    
                if($a->num_rows > 0)
                    return $a->fetch_array(MYSQLI_ASSOC);
    
                return false;
            }   
        }
    
        $Instancia  = new DriveDB("localhost", "root", "roor", "flix");
        $paneles    = $Instancia->paneles();
    ?>
    

    Verify if the password of the root user is roor, I do not know if it has been confused when writing it or if it really is the correct one. To consult the number of rows returned by the query, it is not necessary to put parentheses.

    I have written only the class and the implementation, the final code that you have on the conditionals, you just have to add it below and it will have to work normally.

        
    answered by 29.03.2018 в 17:13