1

我有一个关于从数据库加载多个字段并在 javascript 中使用它们的问题。

这是我的“死亡”表,其中包含以下字段:

- district
- year_1999
- year_2000
- year_2001
- year_2002
- year_2003
- year_2004
- year_2005
- year_2006
- year_2007
- year_2008
- year_2009

现在我想加载字段 year_.... 当 District = 'districtname'

这就是我所拥有的:(PHP)

$host = "localhost";
  $user = "root";
  $pass = "root";

  $databaseName = "testdatabase";
  $tableName = "deaths";

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------

  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);

  //--------------------------------------------------------------------------
  // 2) Query database for data
  //--------------------------------------------------------------------------
  $result = mysql_query("SELECT year_1999,year_2000,year_2001,year_2002,year_2003,year_2004,year_2005,year_2006,year_2007,year_2008,year_2009 FROM $tableName WHERE district = 'Binnenstad'");          //query

  $data = array();
  while ( $row = mysql_fetch_row($result) )
  {
    $data[] = $row;
  }
  echo json_encode( $data );

Javascript:

$(function () 
  {
    //-----------------------------------------------------------------------
    // 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
    //-----------------------------------------------------------------------
    $.ajax({                                      
      url: './api4.php',                  //the script to call to get data          
      data: "",                        //you can insert url argumnets here to pass to api.php
                                       //for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(rows)          //on recieve of reply
      {
        //--------------------------------------------------------------------
        // 3) Update html content
        //--------------------------------------------------------------------

        for (var i in rows)
        {
          var row = rows[i];          

          var data = row[0];

          $('#districts ul').append("<li>" + data + "</li>")
                  .append();
        } 
      } 
    });
  }); 

但这仅显示第一列数据(year_1999)。

我怎样才能解决这个问题?

4

1 回答 1

3

而是for (var i in rows) ...尝试:

更新。仔细看看你的 php,试着something like每年画一下:

$.each(rows, function(i, data) {
    $.each(data, function(j, year) {
        $('#districts ul').append("<li>" + year + "</li>")

    });
});
于 2013-01-19T12:18:17.273 回答