使用MySQLi循环数据库结果

时间:2015-01-17 16:25:11

标签: php mysql mysqli

我正在尝试从MySQL中提取注册信息列表。我正在将查询更改为面向对象,当我这样做时,结果不会显示在表中。没有显示错误消息。

<?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(); ?>

我做错了什么?我搜索并搜索过,找不到任何解决问题的方法。

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>

当我试图改进你已经提供的表格所提供的代码时,你提供的代码是有效的,我仍然不会填写表格。

1 个答案:

答案 0 :(得分:1)

如果您使用的是程序界面mysqli_fetch_assoc(),则需要提供mysqli_result

while ($row = mysqli_fetch_assoc($stmt)) { 

如果您使用面向对象的界面,那么只需将其用作其方法:

while($row = $stmt->fetch_assoc()) {

修:

<?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; ?>