2012-04-30 86 views
0

我想查詢我的數據庫並創建值以供以後在另一個函數中使用。我的第一個函數(get_users())應該查詢請求數據庫並查找爲特定的global_id列出的所有用戶 - 此查詢總是最多有4個用戶。然後我想使用第二個函數(get_results()),並將從第一個函數(get_users())中檢索的值插入到第二個函數中。換句話說,我需要在第二個函數中將users1,2,3,4放入get_results($ user1,$ user2,$ user3,$ user4)中。使用第二個函數的一個mysql函數的值

希望有人能幫忙!這裏是我的功能:

 function get_users($global_id) 
    { 
     $result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'"); 
     $row = mysql_fetch_row($result); 
     $user1 = $row[0]; 
     $user2 = $row[0]; 
     $user3 = $row[0]; 
     $user4 = $row[0]; 
    } 
    function get_results($user1, $user2, $user3, $user4) 
    { 
     $result = mysql_query("SELECT * FROM results WHERE username != '$user1' 
     AND username != '$user2' 
     AND username != '$user3' 
     AND username != '$user4' 
     ORDER BY distance"); 
     ...more stuff to do here with the query 
    } 

感謝

+0

這可能是值得考慮'JOIN':http://www.w3schools.com/sql/sql_join.asp – rjz

+0

是否有良好的重複'$行[0]'定義'$ USER1時'到'$ user4'? –

+0

是的,我認爲它可能是$ row [1],$ row [2],$ row [3],$ row [4]? –

回答

1

呼叫的第一個內部的第二功能:

function get_users($global_id) 
    { 
     $result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'"); 
$count = 0;   
while($row = mysql_fetch_array($result)) 
     { 
     $user[$count] = $row; 
     $count++; 
     } 
    get_results($user[0],$user[1],$user[2],$user[3]); 
    } 

    function get_results($user1, $user2, $user3, $user4) 
    { 
     $result = mysql_query("SELECT * FROM results WHERE username != '$user1' 
     AND username != '$user2' 
     AND username != '$user3' 
     AND username != '$user4' 
     ORDER BY distance"); 
     ...more stuff to do here with the query 
    } 

你甚至可以簡化get_results功能有一個變量數組,而不是4 varialbles

function get_results($users) 
     { 
      $result = mysql_query("SELECT * FROM results WHERE username != '".$users[0]."' 
      AND username != '".$users[1]."' 
      AND username != '".$users[2]."' 
      AND username != '".$users[3]."' 
      ORDER BY distance"); 
      ...more stuff to do here with the query 
     } 

而且你應該在第一個函數中像這樣調用它

get_results($users); 
0

將值作爲參數發送到另一個函數。

function get_users($global_id) 
{ 
    $result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'"); 
    $row = mysql_fetch_row($result); 
    $user1 = $row[0]; 
    $user2 = $row[0]; 
    $user3 = $row[0]; 
    $user4 = $row[0]; 

    //now send 
    get_results($user1, $user2, $user3, $user4); 
} 
相關問題