I am creating an application that fetches photos that users upload across my service and display it in a worldwide newsfeed.
I am having trouble creating the PHP MYSQL Code for a specific user with a specific user ID though, rather than everyone.
My database has the following in the photos table: IdPhoto, IdUser, title.
Here is how I am grabbing the worldwide feed (This works):
//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');
}
}
I would like to do a similar deal. But for a specific user with a specific ID. Any suggestions on the MYSQL code / PHP code required for this?
I've tried the following for my profile function which didn't seem to work:
SELECT IdPhoto, IdUser,title, username FROM photos WHERE IdUser=$IdUser;
I am also calling the functions in my index.php file using cases:
case "stream":
stream((int)$_POST['IdPhoto']);
break;
Any idea what that would need to be for my profile function too?
The query should be:
SELECT IdPhoto, IdUser, title FROM photos WHERE IdUser = $IdUser;
Be sure to use single quotes when you are declaring variables; i.e. IdUser='$IdUser';.