Medoo - Include MySQL query directly

0

I have a file with a query model that I can not understand and I need to be able to change it, but changing the model stops working so it should be the same as this one, but modifying the query or at least functioning like this, this is the current file and the query gives the result:

<?php
require_once('medoo.php');
require_once('../../php/inc.php');
$database = new medoo([
    'database_type' => 'mysql',
    'database_name' => $DB,
    'server' => $SERVIDOR,
    'username' => $USUARIO,
    'password' => $CLAVE,
    'charset' => 'utf8'
]);
$tbl = $_GET['tabla'];
$accion = $_GET['accion'];
$tabla = $TABLA[$tbl];
$columnas['usuarios'] = ['id', 'Nombre', 'Cedula', 'Empresa', 'FechaReg', 'activo'];
var_dump($TABLA['usuarios']);
$data['usuarios'] = $database->select($TABLA['usuarios'], $columnas['usuarios']);
if ($accion == 'leer') {
    $salida = [];
    if ($tbl = 'usuarios') {
        foreach($data[$tbl] as $llave => $dato) {
            if($dato['activo']) {
                array_pop($dato);
                array_push($salida, $dato);
            }
        }
        print_r(json_encode($salida, JSON_UNESCAPED_UNICODE));
    } 
}
function obtenerID($id, $tabla, $campo){
    foreach($tabla as $dato) {
        if ($dato['id'] == $id) {
            return $dato[$campo];
        }
    }
}
?>

But I must modify that query and do it this way

SELECT * FROM usuarios u LEFT JOIN cursos r ON r.CodUsuario=u.id LEFT JOIN listadocursos c ON r.CodCurso=c.id ORDER BY u.id ASC

In the medoo.php file there is the function

public function select($table, $join, $columns = null, $where = null)
{
    $query = $this->query($this->select_context($table, $join, $columns, $where));
    return $query ? $query->fetchAll(
        (is_string($columns) && $columns != '*') ? PDO::FETCH_COLUMN : PDO::FETCH_ASSOC
    ) : false;
}
    
asked by jorgnv 25.04.2018 в 04:02
source

1 answer

0
  

I think what will be most comfortable for you is to use the method directly    query from medoo

What you put in the first sentence is a piece of code in [PHP] ( link ).

The second thing is a query SQL They are completely different languages.

In this case, what you have in the code in PHP, is a query based on data through medoo , which is a database adapter for PHP. Here is the documentation of how to make inquiries with Medoo

And arrived, here, I think the logical thing will be:

<?php
require_once('medoo.php');
require_once('../../php/inc.php');
$database = new medoo([
    'database_type' => 'mysql',
    'database_name' => $DB,
    'server' => $SERVIDOR,
    'username' => $USUARIO,
    'password' => $CLAVE,
    'charset' => 'utf8'
]);
$tbl = $_GET['tabla'];
$accion = $_GET['accion'];
$tabla = $TABLA[$tbl];
$columnas['usuarios'] = ['id', 'Nombre', 'Cedula', 'Empresa', 'FechaReg', 'activo'];
var_dump($TABLA['usuarios']);

// Aquí es donde realizas la consulta con el método query en vez de construyéndola de manera compleja

$data['usuarios'] = $database->query("SELECT * FROM usuarios u LEFT JOIN cursos r ON r.CodUsuario=u.id LEFT JOIN listadocursos c ON r.CodCurso=c.id ORDER BY u.id ASC")->fetchAll();


if ($accion == 'leer') {
    $salida = [];
    if ($tbl = 'usuarios') {
        foreach($data[$tbl] as $llave => $dato) {
            if($dato['activo']) {
                array_pop($dato);
                array_push($salida, $dato);
            }
        }
        print_r(json_encode($salida, JSON_UNESCAPED_UNICODE));
    } 
}
function obtenerID($id, $tabla, $campo){
    foreach($tabla as $dato) {
        if ($dato['id'] == $id) {
            return $dato[$campo];
        }
    }
}
?>

I hope it helps you

    
answered by 25.04.2018 в 04:37