2014-09-19 60 views
0

我有兩個表,名稱和推入結果照片管理使用連接和陣列

名稱表:

enter image description here

照片管理表:

enter image description here

我想在php中編寫一個函數(通過使用sql),它得到一個id並返回如下結果:

//假設我發送1到這個功能...

結果=陣列( 「名稱」=> 「NAME1」, 「圖片」=> 「url6-url7」);

URLS中有連字符。

有幫助嗎?

+4

你到目前爲止試過了什麼? – 2014-09-19 10:15:31

+0

我只知道:$ sql =「select * from Names,Pics where Names.ID = Pics.Name_ID」; – 2014-09-19 10:25:48

+0

'select * from Names,Pics where Names.ID = Pics.Name_ID and Names.ID = $ inputId' – Sadikhasan 2014-09-19 10:33:20

回答

1
function whatIsThis($id){ 
    global $db; 
    $sql = "select name, pics from names,pics where names.ID = pics.Name_ID AND names.ID = ".$id; 
    $res = $db->query($sql); 
    // Logic starts here... 
    foreach($res as $rk=>$rv){ 
     $result['name'] = $rv['name']; 
     $r[] = $rv['pics']; 
    } 
    $result['pics'] = implode('-',$r); 
    return $result; 
} 
$iWant = whatIsThis('1'); 

我用我自己的數據庫類數組的形式輸出結果對於查詢,你可以使用自己的..
$iWant將返回數組,因爲你需要!

0

考慮在$ SQL你的SQL結果,用var_dump打印結果,你會得到你想要:)

1
`Try this code its working 

SELECT GROUP_CONCAT(category.pics SEPARATOR '-') as newpics, company.id as searchid FROM `category` inner join company on (company.id = category.cmp_id) where category.cmp_id = 2