Foreach travels PDOStatement object in some cases and in others not. Why is it?

0

I'm working on a PHP project with connection to MySQL , but I have a problem, when I do select , in some cases foreach can go through the PDOStatement object and in others do not, I do not know if I owe it to the consultation or what it may be. I have tried doing debugging of the code and simply skips the cycle foreach in some queries. This is the general query:

/* $seleccionado es el servicio seleccionado que cambia según lo que 
escoja el cliente, es un select en HTML*/
"SELECT nombre, descr, imagen FROM servicio WHERE nombre = 
'$seleccionado'"

Works when $ selected takes any of the following values:
Solutions In Microwave Links
Deployment of Network 4G, 3G
Equipment Rental

Stop working when $ selected takes any of the values:
Interference Analysis
Signal Coverage Solutions

The problem is in the function process_result ($ db_fields) , it is the first foreach which is sometimes skipped with the queries mentioned above.
I leave the code of the connection:
The call stack starts with the function get_data ($ query, $ fields_bd);

<?php
    include('constantes.php');

    class conexion{
        private $conexion;
        private $res;

        //El segundo parametro es un arreglo de cadenas que tiene los 
 campos de la base de datos
        public function obtener_datos($consulta, $campos_bd){
            try{        
                $this->conectar_y_seleccionar_bd(constantes::SERVER, 
 constantes::USER, constantes::PASS, constantes::BD);
                 $this->res = $this->conexion->query($consulta);
                return $this->procesar_resultado($campos_bd);
            } catch(PDOException $e){
                print "Error!: ".$e->getMessage()."</br>";
                die();
            }   
        }

        public function actualizar_datos($consulta){
            try{    
                $this->conectar_y_seleccionar_bd(constantes::SERVER, 
                constantes::USER, constantes::PASS, constantes::BD);
                $this->res = $this->conexion->query($consulta);
             } catch(PDOException $e){
                print "Error!: ".$e->getMessage()."</br>";
                die();
             }  
        }

        private function conectar_y_seleccionar_bd($server, $user, $pass, $bd){
            $this->conexion = new PDO("mysql:host=$server;dbname=$bd", $user, $pass);
        }

        private function procesar_resultado($campos_bd){
            $arreglo_resultado = array();
             foreach($this->res as $fila){
                $vector = array();
                foreach ($campos_bd as $campo) {
                array_push($vector, $fila[$campo]);
                }
                array_push($arreglo_resultado, $vector);
            }
            return $arreglo_resultado;                  
        }
    }
?>
    
asked by Luis De La Peña 20.12.2017 в 17:31
source

1 answer

0

I found the solution and it was to code the POST entries with the function utf8_decode.
What happened was that the words when using tilde and special characters were sent to the database as such and that is why it was not found such a consultation.

utf_decode($_POST['seleccionado'])

Now all that remains is to fix the SQL vulnerability!

    
answered by 20.12.2017 / 22:19
source