Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
我一直在尝试制作一个使用mysql将三列记录输出到2-D列表中的php函数。但是,这样做时,每次数据库中的第一个值都会被跳过。我又读了一次代码,似乎找不到任何问题。问题不在于最后的输出循环,无论我将回声放在何处,数据库中的第一条记录都不会显示。我正在使用XAMPP来运行代码。在Chrome浏览器中。

<?php

function countHouse() {
    $connection = mysql_connect('localhost','','');
    mysql_select_db('sports');

    $query = "SELECT ID, house, housePoints FROM sportsday WHERE housePoints     != 0";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    $data = array();
    while($row = mysql_fetch_array($result)) {
        $data[] = array($row['house'],$row['housePoints'],$row['ID']);
    }
    mysql_close();
    return $data;
}

//Output, not a problem except $data is missing first term
$data = countHouse();
for ($i=0; $i<count($data); $i++) {
    echo $data[$i][2] . " " .$data[$i][0] . $data[$i][1];?><html><br></html>     <?php
}
?>

最佳答案

请注意,您正在读取while之前的第一行:

// get rid of this line of code
//$row = mysql_fetch_array($result);
$data = array();
while($row = mysql_fetch_array($result)) {
    $data[] = array($row['house'],$row['housePoints'],$row['ID']);
}

10-04 11:21