2012-05-15 145 views
-2

下面顯示的代碼用於手動填充數組。PHP:從結果集中填充數組

<?php 
     include_once 'include/DatabaseConnector.php'; 
     $data = array(
      array(0,array("111",' EE112',' AA','FT445'),"2004-03-01 10:00","2004-03-01 14:00"), 
      array(1,array("111",' BC124',' RYA','FE675'),"2004-03-01 16:00","2004-03-01 18:00"), 
      array(2,array("11",' BE225',' FA','AE667'),"2004-03-01 09:00","2004-03-01 10:00"), 
      array(3,array("11",' TC828',' BA','FF745'),"2004-03-01 06:00","2004-03-01 08:00") 
     ); 
?> 

現在我想從數據庫中填寫此陣:

$query1="SELECT * FROM MyData;"; 
$result1=DatabaseConnector::ExecuteQueryArray($query1); 

<?php foreach ($result1 as $row):?> 
          <tr> 
          <td><?php echo $row['resReg']; ?></td> 
          <td><?php echo $row['resTitle']; ?></td> 
          <td><?php echo $row['resAvailability'] ? 'Yes' : 'No';?></td> 
          </tr> 
          <?php endforeach;?> 

如何RESULT1的列分配給數組的列?

+2

哪有不回答任何*的線索,所有的*關於什麼'$ result1'是什麼? – Jon

+1

請查看更新後的代碼,儘管基於其他數據的示例也會有所幫助。 –

+0

這仍然是痛苦的不足。查詢獲取哪些字段?他們與「手動」數組字段的關係是什麼?說到這一點,手冊中的第1列包含數組。這些是什麼數組?他們的內容是什麼?他們如何映射到查詢返回的內容? – Jon

回答

2

我說得對不對假設你想要做這樣的事情:

<?php 

$query1 = "SELECT name,color1,color2 FROM MyPets;"; 
$result1 = DatabaseConnector::ExecuteQueryArray($query1); 
$petArray = array(); 

foreach($result1 as $pet): 
    $petArray[] = array('name' => $pet['name'], 
         'colors' => array(
          $pet['color1'], 
          $pet['color2']) 
        ); 
?>