2

我想从下表中获取所有行时遇到问题date

id      name       username    app      sdate    edate   
===    =======     ========    ===      =====    ======  
 1      tanvir      tanvir      1       2012      2012       
 2      Ranvir      Ranvir      1       2011      2013      
 3      john        john        2       2011      2012     
 4      Rakib       Rakib       1       2011      2012  

我使用以下 MySQL 查询:

$date = mysql_query("select * from `date`");
$date_row = mysql_fetch_array($date);

但它只返回一行。

如何将所有行放入二维数组中?

4

2 回答 2

11

Well of course it's only returning one row, that's what mysql_fetch_array does:

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

Now, to build an array of your rows, you just have to iterate over them:

$all_rows = array();
while($row = mysql_fetch_assoc($date)) $all_rows[] = $row;

Notice how I use mysql_fetch_assoc here. You should NEVER use mysql_fetch_array unless you very specifically need both numeric and associative indices.

于 2013-04-25T13:52:05.017 回答
0

尝试这个:

$date =  getQueryData("select * from `date`");
function getQueryData($sQuery)
{
    $result = mysql_query($sQuery); 
    $rows = Array();
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
       $rows[] = $row;
    }
    @mysql_free_result($result);    
    return $rows;
}
于 2013-04-25T14:36:52.123 回答