I am wondering how I can make this work,
I tried:
$query = "SELECT author, SUM(likes) FROM posts WHERE author = '$usern' GROUP BY likes order by SUM(likes) + 0 desc";
$result = mysql_query($query) or die(mysql_error());
But it just gives me number of likes of first post, not from all posts in-one.
So, I need to get all likes from all posts where username is $usern
Database rows: id, likes, author, date
I need to output one number, e.g. 50 if the author has 5 posts and on every post 10 likes
If you really only want the total number of likes for $usern, this simple query will already suffice:
$query = "SELECT SUM(likes) FROM posts WHERE author = '$usern'";
You only need a GROUP BY only if you want to retrieve this information for multiple authors at once:
$query = "SELECT author, SUM(likes) FROM posts GROUP BY author ORDER BY SUM(likes) + 0 DESC";
SELECT `author`, SUM(`likes`) FROM `posts` WHERE `author` = $user;
this query will show you how many likes have got $user under all his posts
Related
I am trying to get all the posts the user has liked displaying and not all posts.
I have database tables as follows:
likes (columns (int): likeID, likeBy, likeOn)
posts (columns: postID, postBy, text, likeCount)
users (Columns: userID, username)
Here is my code to get the users liked post (at the moment it gets all liked posts on database and not user liked posts)
public function likes($user_id, $num){
$stmt = $this->pdo->prepare("SELECT * FROM `likes`, `posts`, `users` WHERE `likeOn` = `userID` and `user_id` = `postBy` ORDER BY `likeOn` DESC");
$stmt->bindParam(":num", $num, PDO::PARAM_INT);
$stmt->execute();
$tweets = $stmt->fetchAll(PDO::FETCH_OBJ);
any ideas on how to get the posts liked by the user and not all liked posts?
select p.postID from posts p inner join likes l on l.likeOn=p.postID inner join users u on u.userID = l.likeBy;
I think the above query will work for you try it in MySQL database.
As others have said in comments, your schema is not structured in a proper way to have solid relations between tables.
But, given your current structure (and assuming you are updating the likes count, and that posts.postBy contains the user id), you could try something like:
SELECT posts.* FROM posts AS p WHERE COUNT(p.likeCount) > 0 AND p.postBy = the_user_id_variable
If you need posts only, and the userId is contained inside the post row, you do not need to join different tables.
I think this will work.
I changed your query so the statement will return the posts and likes coupled together with the inner join
public function likes($user_id, $num){
$stmt = $this->pdo->prepare("SELECT * likes INNER JOIN posts ON likes.likeOn = posts.postID WHERE likes.likeBy = ? ORDER BY `likeOn` DESC");
$stmt->bind_param("i",$user_id);
$stmt->bindParam(":num", $num, PDO::PARAM_INT); // Don't know what this does
$stmt->execute();
$tweets = $stmt->fetchAll(PDO::FETCH_OBJ);
}
Edit: removed the User table from the query because the userid allready existed in the like table (likeBy).
I have a menu, and in this menu there is an option to select posts which have a certain brand name in the database. When clicked no the brand name in the menu I want the user to be directed to the page which has posts with only that brand name in the database.
This is my button url:
A. Lange & Söhne
This is my sql call:
if($_GET){
$id1 = $_GET['id'];
$id1 = (int) $id1;
$sql = "SELECT posts.id AS postid,
category.id AS catid,
category.catname,
posts.id,
posts.cat_id,
posts.brand,
posts.auction,
posts.likes,
posts.image,
posts.title,
posts.textbody,
posts.author,
posts.postdate
FROM posts INNER JOIN category ON category.id= posts.cat_id WHERE (posts.brand ='$brand')";
//"SELECT * FROM posts WHERE id='$id1' ORDER BY postdate DESC LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$statusnumrows = mysqli_num_rows($query);
//get set above for ID
}
else if(!$_GET){
$sql = "SELECT posts.id AS postid,
category.id AS catid,
category.catname,
posts.id,
posts.cat_id,
posts.brand,
posts.auction,
posts.likes,
posts.image,
posts.title,
posts.textbody,
posts.author,
posts.postdate
FROM posts INNER JOIN category ON category.id= posts.cat_id WHERE (posts.brand ='$brand') ORDER BY postdate DESC";
It doesnt seem to be working. how can I get the user to go to a list of posts with only one chosen brand. Right now it displays the two posts I have where the BRAND section is left black in the database.
Since you aren't setting $brand = $_GET['brand'] it's actually just doing this in your sql query WHERE (posts.brand = '') make sure you set the variable if you want it to work.
Just from looking at this, it looks like you are not grabbing the GET data. You are sending the query string "?brand=ALange&Söhne" but your GET is looking for "id" instead.
What happens if you change your PHP to:
$brand = $_GET['brand'];
Not sure if you have anything else passed via GET, though. Hope that helps you out.
--Charles
I'm attempting to do a relatively simple MySQL query where I return the favorite posts added by the user. However, my only error is the name of the post int he favorites returns as the user who added the favorite and not the actual user who wrote the post. Below is my database for the favorites and then the query below. Please let me know if you need more information from me as I'd be happy to help in any way.
$username = $_GET['username'];
//initial query
$query = "SELECT body, latitude, longitude, (SELECT username FROM Users WHERE Users.IDUser=Favorites.IDUser) AS username, (SELECT profile_picture FROM Users WHERE Users.IDUser=Favorites.IDUser) AS profile_picture, filename, post_date FROM Posts, Favorites WHERE Posts.IDPosts = Favorites.IDPosts AND Favorites.IDUser=(Select IDUser FROM Users WHERE Users.username=:username) ORDER BY post_date DESC";
$query_params = array(
':username' => $username
);
DB diagram:
![enter image description here][2]
You should have uploaded your database schema for this question and you should've formatted your query to be more readable but anyway I don't know if that's correct, I could not tried it but try and please let me know if it is wrong (I don't know what I'm doing because I cannot see the schema and I don't know what are you trying to do)
SELECT body, latitude, longitude, Users.username,
Users.profile_picture, filename, post_date
FROM Posts, Favorites, Users
WHERE Users.username=:username AND Posts.IDPosts = Favorites.IDPosts
AND Favorites.IDUser = Users.IDUser ORDER BY post_date DESC
Edit
SELECT Users.profile_picture, Users.username, latitude,
longitude, post_date, filename
FROM Users
JOIN Posts ON Posts.IDUser = Users.IDUser
JOIN Favorites ON Posts.IDPosts = Favorites.IDPosts
WHERE Users.username=:username
AND Users.IDUser = Posts.IDUser
AND Posts.IDPosts = Posts.IDPosts
I've successfully managed to output the posts of a users friends...
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) ORDER BY `time` DESC");
This outputs all of the friends posts of the user. However I want to get the posts of the user itself AS WELL as the friends posts...this is what I have tried...
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) AND `user_id`=$session_user_id ORDER BY `time` DESC");
I even tried making another mysql statement just to load the users posts however when I did that, the users posts were being outputted first in the order of the time that they were outputted and then after the users posts were the friends posts shown...
Thanks :)
You actually want an OR condition:
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) OR `user_id`=$session_user_id ORDER BY `time` DESC");
Try:
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) OR `user_id`=$session_user_id ORDER BY `time` DESC");
That will return anything where user_id is in your array; as well as the user's own ID.
If I understand correctly chwhat you are trying to do all you need do is change
And 'user_id = $session_iser_id
To
Or 'user_id = $session_iser_id
i have a online application for wich i require a sort of dashboard (to use the white-space).
There are three tables used for the operation:
1.) categories: id, name
2.) entries: id, name, description, category_id, created, modified
3.) entryimages: id, filename, description, entry_id
on the dashboard i want to show 4-5 entries (with thumbnail images, so i require joins to the entryimages table and the categories table) for each category.
I read through some articles (and threads on s.o.) like this one:
http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
But am still not getting it right, i've tried to first extract all categories and for each and every category build a query and with "all union" attach them to one, but that is not working.
The last version of code i used:
foreach($categories as $id => $name)
{
$query .= "SELECT `entry`.`id`,
`entry`.`name`,
`entry`.`description`,
`entry`.`category_id`,
`entry`.`created`,
`entry`.`modified`,
`entryimages`.`filename`,
`entryimages`.`description`
FROM `entries` as `entry` LEFT JOIN `entryimages` ON `entryimages`.`entry_id` = `entry`.`id`
WHERE `entry`.`category_id` = $id ";
if($i < count($groups))
{
$query .= 'UNION ALL ';
}
$i++;
}
$result = mysql_query($query);
Does anybody know what is the best right to accomplish this operation?
Thanks 1000
On the dashboard if you want to show three entries, the way you are doing is wrong. If my understanding is right, the entire query will be something like
"SELECT `entry`.`id`,
`entry`.`name`,
`entry`.`description`,
`entry`.`category_id`,
`entry`.`created`,
`entry`.`modified`,
`entryimages`.`filename`,
`entryimages`.`description`
FROM `entries` as `entry`
INNER JOIN categories
ON (entry.category_id = categories.id)
LEFT JOIN (SELECT * FROM `entryimages` WHERE `entry_id` = `entry`.`id` LIMIT 1) AS `entryimages`
ON `entryimages`.`entry_id` =`entry`.`id`
ORDER BY `entry`.`created` DESC LIMIT 5";
Your code looks ok to me you should just add a LIMIT clause so that you get just five of them and an ORDER BY clause to get the latest
$query .= "SELECT `entry`.`id`,
`entry`.`name`,
`entry`.`description`,
`entry`.`category_id`,
`entry`.`created`,
`entry`.`modified`,
`entryimages`.`filename`,
`entryimages`.`description`
FROM `entries` as `entry` LEFT JOIN `entryimages` ON `entryimages`.`entry_id` = `entry`.`id`
WHERE `entry`.`category_id` = $id ORDER BY `entry`.`created` DESC LIMIT 5";