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 回答