0

ユーザーが自分のサービスでアップロードした写真を取得し、それを世界中のニュースフィードに表示するアプリケーションを作成しています。

ただし、全員ではなく、特定のユーザー 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;

私のプロファイル機能にも何が必要でしょうか?

4

2 に答える 2

1

変数を宣言するときは、必ず一重引用符を使用してください。つまり、IdUser='$IdUser'; です。

于 2013-07-25T16:29:22.070 に答える
1

クエリは次のようになります。

SELECT IdPhoto, IdUser, title FROM photos WHERE IdUser = $IdUser;
于 2013-07-25T16:29:40.413 に答える