Displaying MySQL in PHP -
so far, have program links phpmyadmin database php script. now, need display things in table "all records," "all contacts last name starts s," "all pet owners," etc. question is: there simpler way insert code php script display information database. right have long echo statement display information. there way can use select * statement display records , simplify code?
<?php $db_hostname='localhost'; $db_username='root'; $db_password=''; $db_database='address book'; $connection = new mysqli( $db_hostname, $db_username, $db_password, $db_database); if ($connection->connect_error) { echo "sorry"; } else { echo "connected!<br><br>"; $sql = "select * people"; $result = $connection->query($sql); if (!$result) die ($connection->error); $n = $result->num_rows; ($i=1; $i<=$n; $i++) { $row = $result->fetch_array(mysqli_assoc); echo "<table> <tr><th>id</th><th>first name</th><th>last name</th> <th>street address</th><th>city</th> <th>state</th><th>zip code</th> <th>email address</th><th>comment</th> <th>number of pets</th></tr>"; echo "<tr><td width=20>" . $row['id'] . "</td><td>" . $row['first name'] . "</td><td width=40>" . $row['last name'] . "</td><td width=200>" . $row['street address'] . "</td><td width=30>" . $row['city'] . "</td><td width=40>" . $row['state'] . "</td><td width=30>" . $row['zip code'] . "</td><td width=40>" . $row['email address'] . "</td><td width=20>" . $row['comment'] . "</td><td width=10>" . $row['number of pets'] . "</td></tr>"; } echo "</table>"; } ?>
you should table structure first insert php codes within structure. e.g:
<?php // put mysqli retrievals codes here ?> <table> <tr> <th>field_1</th> <th>field_2</th> <th>field_3</th> </tr> <?php while ($rows = $result->fetch_array(mysqli_assoc)) { ?> <tr> <td><?php echo $rows['field_1']; ?></td> <td><?php echo $rows['field_2']; ?></td> <td><?php echo $rows['field_3']; ?></td> </tr> <?php } ?> </table>
Comments
Post a Comment