sql >> Database >  >> RDS >> Mysql

Het afhandelen van while-loop en het groeperen van MYSQL PHP-waarden

Probeer mysql_* te vermijden functies. Gebruik mysqli_* . U hoeft ze alleen eerst te groeperen. Beschouw dit voorbeeld:

<?php

$link = new mysqli('localhost', 'test', 'test', 'test');
$query = mysqli_query($link, 'SELECT * FROM author_master ORDER BY author_id');
$books = array();
while($row = $query->fetch_assoc()) {
    $books[$row['author_id']][] = $row;
}
?>

<table border="0" cellpadding="10">
    <thead>
        <tr>
            <th>Author ID</th>
            <th>Author Book</th>
            <th>Rating</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($books as $author_id => $values): ?>
            <tr>
                <td><?php echo $author_id; ?></td>
                <td><?php foreach($values as $author_book) {echo $author_book['author_book'] . '<br/>';} ?></td>
                <td><?php foreach($values as $author_book) {echo $author_book['rating'] . '<br/>';} ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>


  1. SQL SELECT --> csv-bestand

  2. LEN() versus DATALENGTH() in SQL Server

  3. PDO Value Increment PHP, Mysql

  4. Het verschil tussen FIELD() en FIND_IN_SET() in MySQL