从特定用户ID获取流

时间:2013-07-25 16:22:32

标签: php mysql database

我正在创建一个应用程序,用于提取用户通过我的服务上传的照片,并将其显示在全球新闻源中。

我无法为具有特定用户ID的特定用户创建PHP MYSQL代码,而不是每个人。

我的数据库在照片表中有以下内容:IdPhoto,IdUser,title。

以下是我如何抓住全球Feed(这是有效的):

//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;

我也在使用case:

调用index.php文件中的函数
case "stream":
    stream((int)$_POST['IdPhoto']);
    break;

还知道我的个人资料功能需要什么吗?

2 个答案:

答案 0 :(得分:1)

在声明变量时,请务必使用单引号;即IdUser ='$ IdUser';。

答案 1 :(得分:1)

查询应为:

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