Two selects in a while with PDO

Asked

Viewed 257 times

0

Hello, I’m migrating from Mysql to PDO and I’m having difficulty putting in a table two selects.

try{
        $CON = new PDO("mysql:host=localhost;dbname=tcc", 'root', '1234');
    }
    catch (PDOException $e)
    {
        echo "Erro : ".$e->getMessage();
    }

    $select = "select * from MATERIA";
    $resultado = $CON->query($select);

    $select2 = "select * from ALUNO";
    $resultado2 = $CON->query($select2);



    while($row = $resultado->fetch(PDO::FETCH_BOTH) and  $row2=$resultado2->fetch(PDO::FETCH_BOTH)){
        echo "<tr><td>".$row['NOME']."</td><td>".$row2['NOME']."</td>";
    }

The problem is, when the results of query 1 are over, he stops listing the other students, and I’d like him to continue writing them.

  • It doesn’t make much sense what you did. What is your goal?

  • I need to list in a table where the first column are registered students and in the second the subjects registered in the bank.

1 answer

0


As they said in the comments, your code makes little sense. It would be better if you spoke what you want, maybe some print of the project.

Anyway, with a few modifications this will help you.

<?php

try {
    $conn = new pdo('mysql:host=localhost;dbname=tcc', 'root', '1234', [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);
} catch (PDOException $e) {
    echo "Erro: {$e->getMessage()}";
    die;
}

$stmtMaterias = $conn->prepare('SELECT * FROM `MATERIA`');
$stmtAlunos = $conn->prepare('SELECT * FROM `ALUNO`');

try {
    $stmtMaterias->execute();
    $stmtAlunos->execute();
} catch (PDOException $e) {
    echo "Erro: {$e->getMessage()}";
    die;
}

$materias = $stmtMaterias->rowCount() ? $stmtMaterias->fetchAll() : [];
$alunos = $stmtAlunos->rowCount() ? $stmtAlunos->fetchAll() : [];

if (count($alunos) >= count($materias)) {
    foreach ($alunos as $key => $value) {
        echo '<tr>';

        if (isset($materias[$key]['NOME'])) {
            echo "<td>{$materias[$key]['NOME']}</td>";
        }

        if (isset($value['NOME'])) {
            echo "<td>{$value['NOME']}</td>";
        }
    }
} else {
    foreach ($materias as $key => $value) {
        echo '<tr>';

        if (isset($value['NOME'])) {
            echo "<td>{$value['NOME']}</td>";
        }

        if (isset($alunos[$key]['NOME'])) {
            echo "<td>{$alunos[$key]['NOME']}</td>";
        }
    }
}
  • then, I want to show all the data in your columns with a while only, for always having to assign the tags of <td> and <tr>, but if I leave while as posted, it happens this: http://prntscr.com/ffyihq, and if I take the second condition, it gets "middle" right: http://prntscr.com/ffyiti

  • @Gabrielrodrigues I edited the answer. See if now meets what you need.

  • It worked, even though I put the stories in the wrong place, that’s what I wanted! I would like to know how you did it, I didn’t understand half the code you sent, could you give me some tutorial explaining this to a beginner in programming? Again I thank you for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.