簡體   English   中英

將多個相似字段放置在多維數組中-PHP mysql

[英]Place multiple similar fields in multi-dimensional array - php mysql

我想執行一個SQL查詢,如:

select 'tb1'.'f1','tb1'.'f2','tb2'.'f1' from 'tb1','tb2';

現在的問題是我想將其放入PHP數組中,例如:

$result['tb1']['f1'], $result['tb1']['f2'], $result['tb2']['f1']...

任何想法如何實現以上目標? Afaik沒有上述功能。 我想知道最好的簡單方法。 除非必要,否則我不想使用“選擇..作為..”這樣的查詢。

我事先不知道這些字段是什么,因此我無法按照benlumley的答案建議手動分配它們。

謝謝亞歷克

您需要像已經做的那樣選擇數據,然后將其循環以使其成為所需的格式,並且由於字段名稱相同,因此最容易使用別名,否則它們將相互覆蓋。返回的數據(但是您可以使用mysql_fetch_row代替,它返回一個數字索引數組)。

例如:

$sql = "select tb1.f1 as tb1f1,tb1.f2 as tb1f2,tb2.f1 as tb2f1 from tb1,tb2";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    $result['t1']['f1']=$row['tb1f1'];
    $result['t1']['f2']=$row['tb1f2'];
    $result['t2']['f1']=$row['tb2f1'];
}

(該引號在您的sql中也是錯誤的)

那也不會處理多行,但是您的問題有點暗示您只希望有一行?

別名:

$sql = "select tb1.f1,tb1.f2,tb2.f1 from tb1,tb2";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
    $result['t1']['f1']=$row[0];
    $result['t1']['f2']=$row[1];
    $result['t2']['f1']=$row[2];
}

我更喜歡第一個版本,除非您有充分的理由使用第二個版本,因為如果您更改sql或添加字段等,則第二個版本不太可能導致錯誤。

編輯:

從下面的響應中獲取元數據的想法...。

<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('dbname');
$result = mysql_query('select tb1.f1, tb1.f2, tb2.f1 from tb1, tb2');
$meta = array();
for ($i = 0; $i < mysql_num_fields($result); ++$i) {
  $meta[$i] = mysql_fetch_field($result, $i);
}
while ($row = mysql_fetch_row($result)) {
   foreach($row as $key=>$value) {
     $out[$meta[$key]->table][$meta[$key]->name]=$value;
   }
}

似乎完全可以滿足您的要求-盡管您一次只能獲得一行。

輕松更新以在數組上存儲具有另一維的多行:

更改:

$out[$meta[$key]->table][$meta[$key]->name]=$value;

至:

$out[][$meta[$key]->table][$meta[$key]->name]=$value;

既然您說無法指定列別名,並且無法事先知道查詢的字段,所以建議使用mysql_fetch_field()獲取元數據信息的解決方案:

<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('dbname');
$result = mysql_query('select tb1.f1, tb1.f2, tb2.f1 from tb1, tb2');
for ($i = 0; $i < mysql_num_fields($result); ++$i) {
  $meta = mysql_fetch_field($result, $i);
  print_r($meta);
}

您可以從此元數據信息中提取表名和列名,即使查詢中有多個具有相同名稱的列也是如此。

PHP的ext / mysqli支持類似的函數mysqli_stmt::result_metadata() ,但是您說您事先不知道查詢中的字段數,這使得使用mysqli_stmt::bind_result()變得很尷尬。

PDO_mysql目前似乎不支持結果集元數據。


上面腳本的輸出如下。

stdClass Object
(
    [name] => f1
    [table] => tb1
    [def] => 
    [max_length] => 1
    [not_null] => 0
    [primary_key] => 0
    [multiple_key] => 0
    [unique_key] => 0
    [numeric] => 1
    [blob] => 0
    [type] => int
    [unsigned] => 0
    [zerofill] => 0
)
stdClass Object
(
    [name] => f2
    [table] => tb1
    [def] => 
    [max_length] => 1
    [not_null] => 0
    [primary_key] => 0
    [multiple_key] => 0
    [unique_key] => 0
    [numeric] => 1
    [blob] => 0
    [type] => int
    [unsigned] => 0
    [zerofill] => 0
)
stdClass Object
(
    [name] => f1
    [table] => tb2
    [def] => 
    [max_length] => 1
    [not_null] => 0
    [primary_key] => 0
    [multiple_key] => 0
    [unique_key] => 0
    [numeric] => 1
    [blob] => 0
    [type] => int
    [unsigned] => 0
    [zerofill] => 0
)

使用簡短的表名前綴字段名是實現它的一種好方法,而無需在select中創建別名。 當然,您不會獲得所描述的確切結構,但是每個字段在結果數組中都有其唯一的命名索引。 例如,假設您有表用戶,並且該用戶有一個名稱,然后將此字段稱為usr_name。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM