Reputation: 9
I am trying to pull a list of registration information from MySQL. I'm changing the query over to object oriented and when I did this, the results are not being displayed inside the table. No error messages are being displayed.
<?php
$sql = "SELECT * FROM `prereg`";
if (!$stmt = $mysqli->query("SELECT * FROM prereg")) {
echo "Query Failed!: (" . $mysqli->errno . ") ". $mysqli->error;
}else{
while ($row = mysqli_fetch_assoc()) {
?>
<tr>
<!-- <td>
<?php echo $row['UID']; ?>
</td> -->
<td>
<?php echo $row['Guradian']; ?>
</td>
<td>
<?php echo $row['Number']; ?>
</td>
<td>
<?php echo $row['Phone']; ?>
</td>
<td>
<?php echo $row['Name']; ?>
</td>
<!-- <td>
<?php echo $row['Address']; ?>
</td> -->
<td>
<?php echo $row['City']; ?>
</td>
<td>
<?php echo $row['State']; ?>
</td>
<!-- <td>
<?php echo $row['Zip']; ?>
</td>
<td>
<?php print $row['Make']; ?>
</td>
<td>
<?php print $row['Ms']; ?>
</td> -->
<td>
<?php print $row['Fuel']; ?>
</td>
<td>
<?php echo $row['Class1']; ?>
</td>
<td>
<?php print $row['Class2']; ?>
</td>
<td>
<?php print $row['Class3']; ?>
</td>
</tr>
<?php } }
$stmt->free(); ?>
What am I doing wrong? I've searched and searched and I cannot find anything that fixes the problem.
REVISED:
<?php
$stmt = $mysqli->query("SELECT * FROM prereg");
if($stmt->num_rows > 0):
while ($row = $stmt->fetch_assoc()):
?>
<tr>
<!-- <td>
<?php echo $row['UID']; ?>
</td> -->
<td>
<?php echo $row['Guradian']; ?>
</td>
<td>
<?php echo $row['Number']; ?>
</td>
<td>
<?php echo $row['Phone']; ?>
</td>
<td>
<?php echo $row['Name']; ?>
</td>
<!-- <td>
<?php echo $row['Address']; ?>
</td> -->
<td>
<?php echo $row['City']; ?>
</td>
<td>
<?php echo $row['State']; ?>
</td>
<!-- <td>
<?php echo $row['Zip']; ?>
</td>
<td>
<?php print $row['Make']; ?>
</td>
<td>
<?php print $row['Ms']; ?>
</td> -->
<td>
<?php print $row['Fuel']; ?>
</td>
<td>
<?php echo $row['Class1']; ?>
</td>
<td>
<?php print $row['Class2']; ?>
</td>
<td>
<?php print $row['Class3']; ?>
</td>
</tr>
<?php endwhile; endif; ?>
</table>
<?php
$stmt = $mysqli->query("SELECT * FROM prereg");
if($stmt->num_rows > 0): ?>
<table>
<?php while($row = $stmt->fetch_assoc()): ?>
<tr>
<?php foreach($row as $val): ?>
<td><?php echo $val; ?></td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
</center>
when trying to retrofit the code you have provided with the table I already had, the code you gave works, the table I have still wont populate.
Upvotes: 1
Views: 373
Reputation: 41885
If you are using the procedural interface mysqli_fetch_assoc()
, you need to provide the mysqli_result
.
while ($row = mysqli_fetch_assoc($stmt)) {
If you'd use the object oriented interface, then just use it as its method:
while($row = $stmt->fetch_assoc()) {
Revised:
<?php
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
$stmt = $mysqli->query('SELECT * FROM prereg');
?>
<?php if($stmt->num_rows > 0): ?>
<table>
<?php while($row = $stmt->fetch_assoc()): ?>
<tr>
<?php foreach($row as $val): ?>
<td><?php echo $val; ?></td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
<?php else :?>
<p>Table empty</p>
<?php endif; ?>
Upvotes: 1