2013-07-25 62 views
0

我正在創建一個應用程序,該應用程序可提取用戶通過我的服務上傳的照片並將其顯示在全球新聞源中。從特定用戶ID抓取流

我無法爲具有特定用戶ID的特定用戶創建PHP MYSQL代碼,但不是每個人。

我的數據庫在照片表中有以下內容:IdPhoto,IdUser,title。

這裏是我我抓住了全球飼料(這工作):

//stream API 
// 
// there are 2 ways to use the function: 
// 1) don't pass any parameters - then the function will fetch all photos from the database 
// 2) pass a photo id as a parameter - then the function will fetch the data of the requested photo 
// 
// Q: what "$IdPhoto=0" means? A: It's the PHP way to say "first param of the function is $IdPhoto, 
// if there's no param sent to the function - initialize $IdPhoto with a default value of 0" 

function stream($IdPhoto=0) { 

if ($IdPhoto==0) { 

    // load the last 50 photos from the "photos" table, also join the "login" so that you can fetch the 
    // usernames of the photos' authors 
    $result = query("SELECT IdPhoto, title, l.IdUser, username FROM photos p JOIN login l ON (l.IdUser = p.IdUser) ORDER BY IdPhoto DESC LIMIT 50"); 

} else { 
    //do the same as above, but just for the photo with the given id 
    $result = query("SELECT IdPhoto, title, l.IdUser, username FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto='%d' LIMIT 1", $IdPhoto); 
} 

if (!$result['error']) { 
    // if no error occured, print out the JSON data of the 
    // fetched photo data 
    print json_encode($result); 
} else { 
    //there was an error, print out to the iPhone app 
    errorJson('Photo stream is broken'); 
} 
} 

我願做一個類似的協議。但是對於具有特定ID的特定用戶。對此需要的MYSQL代碼/ PHP代碼的任何建議?

我已經試過了我輪廓功能這似乎不工作如下:

SELECT IdPhoto, IdUser,title, username FROM photos WHERE IdUser=$IdUser; 

我使用的情況下還調用函數在我的index.php文件:

case "stream": 
    stream((int)$_POST['IdPhoto']); 
    break; 

任何想法對我的配置文件功能也需要什麼?

+0

什麼*類型*是'IdPhoto'?試試'WHERE IdUser ='$ IdUser';' – user1477388

+0

@ user1477388請參閱上面的說明我做了什麼:)這是一個整數 – qweqweqwe

+0

將您的'$ result'和實際查詢分開,以便您可以在發送之前輸出實際查詢到'query()'方法。在查詢之前告訴我們查詢的價值。 – user1477388

回答

1

查詢應該是:

SELECT IdPhoto, IdUser, title FROM photos WHERE IdUser = $IdUser; 
1

在聲明變量時務必使用單引號;即IdUser ='$ IdUser';.

+0

因爲它是一個整數,所以不需要使用單引號。 – Simone