Two results to one mysql statement - php

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

Related

Count all values from mysql

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

How to get data from a table inside a while loop from another table?

The user_id can be found on the replies table but also in the profiles table. The profiles table has the real_name column.
Below I get all the replies for a specific article_id. My question is how do I echo the real_name of the commenters that is saved into the profiles table ?
I am consider the performance of this.
$replies = mysql_query("select * from replies where article_id = '$row[id]' order by timestamp desc");
while($reply = mysql_fetch_assoc($replies)) {
I can echo the comments from the replies table, but how do I echo the real_name of each?
}
Try That :
$replies = mysql_query("select * from replies,profiles where article_id = '$row[id]' AND replies.user_id=profiles.id order by timestamp desc");
Good luck !

PHP, SQL - getting fetch where table id = user id and count other table where row is = user id

Thanks for helping, first I will show code:
$dotaz = "Select * from customers JOIN contracts where customers.user_id ='".$_SESSION['user_id']."' and contracts.customer_contract = ".$_SESSION['user_id']." order by COUNT(contracts.customer_contract) DESC limit $limit, $pocetZaznamu ";
I need to get the lists of users (customers table) ordered by count of contracts(contracts table)
I tried to solve this by searching over there, but I can't... if you help me please and explain how it works, thank you! :) $pocetZanamu is Number of records.
I need get users (name, surname etc...) from table customers, ordered by number of contracts in contracts table, where is contract_id, customer_contract (user id)..
This should do it where is the column name you are counting.
$id = $_SESSION['user_id'] ;
$dotaz = "Select COUNT(`customer_contract`) AS CNT, `customer_contract` FROM `contracts` WHERE `user_id`=$id GROUP BY `customer_contract` ORDER BY `CNT` DESC";
Depending on what you are doing you may want to store the results in an array, then process each element in the array separately.
while ($row = mysqli_fetch_array($results, MYSQL_NUM)){
$contracts[$row[1]] = $row[0];
}
foreach ($contracts AS $customer_contract => $count){
Process each user id code here
}
Not sure what you are counting. The above counts the customer_contract for a table with multiple records containing the same value in the customer_contract column.
If you just want the total number of records with the same user_id then you'd use:
$dotaz = "Select 1 FROM `contracts` WHERE `user_id`=$id";
$results = $mysqli->query($dotaz);
$count = mysql_num_rows($results);

wall updates can be seen only by friends

i'm creating a wall system where you can post updates on the index page and i wanted the posts to be seen only by friends of the user, i almost made my code work but i still have a problem ,but before i mention my problem if there is a better way to do it please share it with me , i will show you my code
first i have two tables for the code
'my_friends' : 'id' ,'username','friend'
'statues' : 'p_id','post','f_name','userip','date_created'
this code is in post.php then it will be included in the index page
//Check for user's friends
$check = mysql_query("select * from `my_friends` where `username` = '".$username."' order by `id` desc ");
//Get users friends
while($get_users_friends = mysql_fetch_array($check))
{
$show_more_button = 1;
$result = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS TimeSpent FROM `statues` WHERE `f_name` = '".mysql_real_escape_string(strip_tags($get_users_friends["friend"]))."' ");
}
while($row = mysql_fetch_array($result))
{
$comments = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS CommentTimeSpent FROM comments where post_id = ".$row['p_id']." order by c_id asc"); ?>
<div class="friends_area" id="record-<?php echo $row['p_id']?>">
<label style="float:left" class="name">
<b><?php echo $row['f_name']; ?></b><br>
<span>
</span><br>
<em><?php echo $row['post'];?></em>
<br clear="all" />
the code doesn't work right, it show the post feed to only one friend, the first added friend of the user ,in other words , the sql $result = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS TimeSpent FROMstatuesWHEREf_name= '".mysql_real_escape_string(strip_tags($get_users_friends["friend"]))."' "); select all the statues of only one friend ,when i write echo mysql_real_escape_string(strip_tags($get_users_friends["friend"])),'<br>'; before the sql it shows all the friends but when i use it in the sql it doesn't, please help
Here are some thoughts that maybe can help you
Dont use mysql_* functions they are deprecated use PDO class instead
Dont query for * if you dont need whole the information from the table
Table name is statues
After completing first while your variable $result will hold only the last friend data query cause it gets overritten by the next step of iteration(use $result[] = )
Aggregate info from $get_users_friends to rewrite your query into SELECT p_id, post, f_name, UNIX_TIMESTAMP() - date_created AS TimeSpent FROM statues WHERE f_name IN '.implode(',', $firends_name).
Dont see if you use $comments variable at all
$show_more_button = 1; dont see if it is used at all. and ofc it gets overritten by next iteration step;
your code should look something like:
$check = mysql_query("select friend from `my_friends` where `username` = '".$username."' order by `id` desc ");
while($get_users_friends = mysql_fetch_array($check, MYSQL_ASSOC))
{
$result[] = $get_users_friends['friend'];
}
$friends_data_query = mysql_query("SELECT p_id, f_name, post,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM `statues` WHERE `f_name` IN'".implode(',',$result)."'");
while($friends_data = mysql_fetch_array($friends_data_query,MYSQL_ASSOC)){
But make sure that it is rewritten into PDO. Cause now it is very vulnurable to injections.

SQL Select latest row where value matches

I'm trying to return the row from my database, with the highest UID, where the URL column matches http://urltocheck.com.
I've tried all manner of things I can think of, and this is the closest I can get, but I'm getting an SQL syntax error.
My Table is called Adam, and I have the columns... UID (unique), URL (plus loads more). I'm trying to access the MySQL databse via PHP.
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY `UID` ASC;
LIMIT 1;";
Can anyone help please?
You shoul use order DESC and remove the ";" after ASC
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY `UID` DESC
LIMIT 1";
Try like this. Also, remove ; at this line ORDER BY UID ASC; (didn't noticed that earlier) because of which limit 1 not coming to picture.
SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
and `UID` = (select max(`uid`) from `Adam`)
with the highest UID
You should order by UID desc and limit to 1.
You can also ORDER BY MAX ID.
<?php
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY MAX(`UID`) DESC;";
This is executed faster.
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY MAX(`UID`);";
?>

Categories