How can I interpret this mysql query in the codeigniter model?

0
select nombre,nombre_comando,valor 
from servidor S, configuracion C, configuracionservidor F 
where F.id_servidor = S.id 
and F.id_configuracion=C.id

This query works for me in Mysql but at the moment of writing it in the model of codeigniter I can not show the results of the query in the view

This function will show the information in the table [server configuration]

    public function mostrarSerconf(){
        $q= $this->db->get("select nombre,nombre_comando,valor from servidor,configuracion,configuracionservidor where configuracionservidor.id_servidor=servidor.id and configuracionservidor.id_configuracion=configuracion.id");

        if($q->num_rows()>0){
            return $q;
        }else{
            return false;
        }
    }

This is my driver

public function  servConf(){
   $datos = array('mostrar2'=>  $this->modelo_principal->mostrarSerconf());
   $this->load->view('servConf',$datos);
}

This is what I have in my sight

 <?php
        if ($mostrar2 == null) {
          echo 
          "<h3>"."SIN INFORMACÓN PARA MOSTRAR"."</h3>";
        }else{
            foreach ($mostrar2->result() as $fila) {
              echo "<tr>";
              echo "<td>"."<b>".$fila->id."</b>"."</td>";
              echo "<td>"."<center>".$fila->id_servidor."</center>"."</td>";
              echo "<td>"."<center>".$fila->id_configuracion."</center>"."</td>";
              echo "<td>".$fila->valor."</td>";
              echo "<td>"."<a href=".base_url()."principal/editarServi/".$fila->id.'>Editar</a></td>';
              echo "<td>"."<a href=".base_url()."principal/eliminarServi/".$fila->id.'>Eliminar</a></td>';
               echo "</tr>";
            }
          }
      ?>
    
asked by Neri Dex 11.12.2018 в 20:30
source

2 answers

0

The query would be like this:

$this->db->select('nombre,nombre_comando,valor');
$this->db->from('servidor S, configuracion C, configuracionservidor F');
$this->db->where('F.id_servidor = S.id and F.id_configuracion=C.id');

$q = $this->db->get()->result_array();

For codeigniter the database pattern called active record is used, with which all sql queries will be made. Which makes life a bit easier for you by making queries more secure and works for any database adapter.

In the first line, the fields that you want to appear in the query (select) would be shown. In the from (second line), the tables of the query are specified. In the 3 line, where, the condition is indicated. With result_array () returns an array of all records in the query.

In your case it would be like this:

public function mostrarSerconf(){
        $this->db->select('nombre,nombre_comando,valor');
        $this->db->from('servidor S, configuracion C, configuracionservidor F');
        $this->db->where('F.id_servidor = S.id and F.id_configuracion=C.id');

        $q = $this->db->get();

        if($q->num_rows()>0){            
            return $q->result_array();
        }else{
            return false;
        }
    }

And in the template you should change the foreach:

 foreach ($mostrar2 as $fila) {
    
answered by 11.12.2018 / 21:30
source
0

I have the impression - not knowing much about CI and just looking at the documentation -, that you are confusing the ways in which queries are handled with this Framework.

  

$ this-> db-> get () returns all the results of a table, which   is equivalent to "Run" the query when you built it

So your query should have this structure

$this->db->select('t1.col, t2.col');
$this->db->from('tabla1 AS t1, tabla2 AS t2');

$where = array('t1.col ' => $filtro , 't1.otraCol' => $otroFiltro);
$this->db->where($where);

$query = $this->db->get();

I leave the documentation for you to see:)

    
answered by 11.12.2018 в 21:21