2009-02-01 61 views
1

我想執行類似SQL查詢:放置多個類似領域 - PHP的MySQL

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

現在的問題是,我希望把它變成一個數組在PHP中,如:

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

任何想法如何實現上述? Afaik沒有上述功能。我想知道最簡單的方法來做到這一點。除非必要,否則我不想使用「select .. as ..」這樣的查詢。

我事先不知道這些字段是什麼,所以我不能像Benlumley的回答那樣手動指定它們。

謝謝 亞歷克

+0

現在怎麼樣?根據卡爾溫所說的話,我已經更新了它。 – benlumley 2009-02-06 16:47:21

回答

3

你需要選擇你已經在做,並且數據然後遍歷它越來越成所需的格式,而且由於字段具有相同的名稱,其最簡單的要使用別名,或者他們只是在返回的數據中相互覆蓋(但您可以使用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; 
0

,前綴字段名與表名稱的短版是一個好的方法來實現它而不需要在select中創建別名。當然,你沒有得到你描述的確切結構,但是每個字段在結果數組中都有其唯一的命名索引。例如,讓我們假設你有表用戶,並且用戶有一個名字,然後調用這個字段usr_name。

2

既然你說你不能指定列別名,你可以不知道查詢的領域事前,我會用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 
)