• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

212
Vistas
Filling an array to use as a parameter for WHERE clause

I'm trying to fetch all of the position_id's in order to fill an array which I can then pass to the second statement to fetch all of the rows containing a position_id in the array. I'm trying to use this data to fill a dropdown menu.

Am I doing this properly or is there a better way to do this?

// $_POST['aid'] == election_id
if(isset($_POST['aid'])) {
    // Get's the rows with corresponding election_id 
    $stm = $db->prepare("SELECT * FROM position_elections WHERE election_id = " . $_POST['aid']);
    $stm->execute();
    $position_ids = [];
    if ($row = $stm->fetchAll(PDO::FETCH_ASSOC)) 
    {
        $position_id = $row['position_id'];
        array_push($position_ids, $position_id);
    }

    $ids = join("','",$position_ids);
    $stm2 = $db->prepare("SELECT * FROM positions WHERE position_id IN ($ids)");
    $stm2->execute();
    $positions = $stm2->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($positions);

}
almost 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda