Mysql friends online display - php

OK,so I have headech from searching everywhere how to solve this problem..Im trying to show online users,but not all users,only who is in your friend list..
So I have table named users_online and when user logs-in in my website in that table is automaticly created 1 row with date,ip,name,user_id and friend_array (where all user's friends are kept)
So for example I log in in my website and row is created in users_online table. I want to see only my friends online and these friends are stored in friend_array column (1,5,16,5 (thats friends id number)).. How can I take data from friends_array colum and see which one of these id's are logged at the moment,which means which of these id's are existing in user_online table and display on my profile?
I hope is not confusing question...
Well thats my code..all stores in online.php file:
// Checking wheter the visitor is already marked as being online:
$inDB = mysql_query("SELECT user_id FROM users_online WHERE user_id=".$userid);
if(!mysql_num_rows($inDB))
{
// Selects some data required to insert into users_online table from users table
$DB = mysql_query("SELECT img,fname,friend_array FROM users WHERE user_id=".$userid);
while($row=mysql_fetch_assoc($DB))
{
$img = $row['img'];
$fname = $row['fname'];
$farray = $row['friend_array'];
}
mysql_query(" INSERT INTO users_online (user_id,ip,img,fname,friend_array)
VALUES(".$userid.",'".$intIp."','".$img."','".$fname."','".$farray."')");
}
else
{
// If the visitor is already online, just update the dt value of the row:
mysql_query("UPDATE users_online SET dt=NOW() WHERE user_id=".$userid);
}
// Counting all the online visitors:
// Thats where i need to work out with friend array..
// I need to display all online friends only
list($totalOnline) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM users_online"));
// Outputting the number as plain text:
echo $totalOnline;

<?php
$friends = array(1,5,16); // Array of friends
$friendIDs = implode(',', $friends); // Turns array into string for SQL select statement
// Gets only friends info from DB
$sql = "
SELECT date, ip, name, user_id
FROM users_online
WHERE user_id IN (".$friendIDs.")";
?>

Sorted out guys! Thanks for your help!
//Selecting an array from db
$DB = mysql_query("SELECT friend_array FROM users_online WHERE user_id=".$userid);
while($row=mysql_fetch_assoc($DB))
{
$friend_array = $row['friend_array'];
$friends = array($friend_array); // Array of friends
$friendIDs = implode(',', $friends);
}
// Counting all the online friends:
list($totalOnline) = mysql_fetch_array(mysql_query("
SELECT COUNT(*)
FROM users_online
WHERE user_id IN (".$friendIDs.")"));
// Outputting the number as plain text:
echo $totalOnline;
Output was 1 user online as i was loged in with 2 browsers and in users_online table was created 2 rows with id 1 and 2..And id 1 had 3 friends in array (2,5,16), and user with id 2 had (1,3)..So in each of the browsers output was 1..Uray! I hope this question helps someone..Btw Im using update function on msql table and if user logs out,i just delete row in users_online table :)

You should consider changing the database schema. Here's an example:
USER table:
id | name | current_session_id
USER_FRIEND_USER table (contains friends that user has added):
user_id | friend_user_id
SESSION table:
id | user_id | created_at | expires_at
SQL Query to get list of friends for user with id '?':
SELECT u.id, u.name FROM USER u
INNER JOIN USER_FRIEND_USER ufu ON (ufu.friend_user_id = u.id)
INNER JOIN SESSION s ON (s.id = u.current_session_id)
WHERE ufu.user_id = ? AND s.expires_at >= NOW();
Here's SQL Fiddle link: http://sqlfiddle.com/#!2/34f83/1/0

Related

SELECT one data only if all other table datas arre correct

I've been working on this code for a wordpress social media site where you can visualize people of your opposite sex only if they are not your friends (if they are your friends they'll go to another page)
In the php I already can divide men from women, but now I want to also eliminate the men/women whom already are your friend
$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data, WHERE field_id = 3 AND value = 'homme'";
(with this I would get only men), now the info about their friend status is in another table, I tried using WHERE EXIST to comprobe it
$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data, WHERE field_id = 3 AND value = 'homme' AND EXIST (SELECT id {$wpdb->prefix}bp_friends WHERE (initiator_user_id = $user_id AND is_confirmed = 1) OR (friend_user_id = $user_id AND is_confirmed = 1)) ";
But doesn't seems to work.
I just want the user_id from the first table, but if I wanted to extract the friend status (that I dont want to extract, I just want it to corroborate my other info to cut out user_ids) I could apply this query
$already_friends = "SELECT is_confirmed FROM {$wpdb->prefix}bp_friends, WHERE initiator_user_id = $user_id OR friend_user_id = $user_id";
I don't know what is the structure of the tables you are referring to. Based on provided information this might work:
SELECT user_id
FROM {$wpdb->prefix}bp_xprofile_data
WHERE
field_id = 3 AND
value = 'homme' AND
user_id NOT IN (SELECT friend_user_id
FROM {$wpdb->prefix}bp_friends
WHERE initiator_user_id=$user_id AND is_confirmed=1) AND
user_id NOT IN (SELECT initiator_user_id
FROM {$wpdb->prefix}bp_friends
WHERE friend_user_id=$user_id AND is_confirmed=1)
I should acknowledge that this SQL statement looks poor: it is slow and it is hard to read. It should be improved if possible.

Seeing how many posts have been made from male users (Using data from two tables to calculate number)

I have two tables:
users - Where gender information is stored:
id
username
gender
user_thoughts- Where all posts are stored:
id
added_by
What I am trying to do is determine how many posts have been made by male and female users separately. But I am just completely stumped on how to achieve this. So far, I have the following:
<?php
include ("connect.php");
// updating table posts_by_gender whenever admin logs in.
// 1.Get gender of user to compare against the author or the thought
$get_all_users_gen = mysqli_query ($connect, "SELECT gender FROM users WHERE account_type = 'user'");
while ($getting_gen = mysqli_fetch_assoc ($get_all_users_gen)){
$gender = $getting_gen['gender'];
// 2. Get all posts
$getting_thoughts = mysqli_query ($connect, "SELECT username FROM user_thoughts");
$getting_th = mysqli_fetch_assoc ($getting_thoughts);
$added_by = $getting_th['added_by'];
} // while closed
?>
I am just completely confused on what to write after this.
Summary:
Trying to check each row in user_thoughts table, get the added_by data (which is the same as username from users table) and see if that user is male or female.
At the end of the check, I need a variable which holds a number of how many posts belong to male users, and how many to female.
You can do this with a Join... An example given below
SELECT * FROM `user_thoughts` LEFT JOIN users ON user_thoughts.added_by = users.username WHERE users.gender= "male"
This will get all user thoughts by male... Then you can do a mysqli_num_rows($query) to get the count.
You can do the same for females...
However, if you only need the count, it may be adviseable to run
SELECT COUNT(*) AS number FROM `user_thoughts` LEFT JOIN users ON user_thoughts.added_by = users.username WHERE users.gender= "male"
and this would return the number of rows directly.

Select user if the user has data on another table

Good Afternoon.
I am trying to create a list of all the Users in my database that contain data in a certain table.
I have 2 tables.
USERS - Contains the name of all the users
DATA - Contains data relative to the user (IT isn't called DATA obviously).
Let's say i have 3 users:
Pedro;
Armando;
Henrique;
Pedro has data in the second table. "Armando" and "Henrique" Don't have any data in the other table.
I want to print the name of all users that contain data on the "data" table.
I tried to do this:
$query=mysql_query("select nome from users where nome <> 'admin' ORDER BY nome");
while ($whatever=mysql_fetch_array($query, MYSQL_ASSOC)){
foreach ($whatever as $w){
echo $w. ' '; //$w contains the name of all the users.
$queryy = mysql_query("select id from users where nome='$w'");
$idd = mysql_fetch_array($queryy);
}
}
$conta=count($idd);
for ($i=0;$i<$conta;$i++){
echo $idd[$i];
$queryyy=mysql_query("select * from pp where id_user='$idd[$i]'");
}
On the table "pp", the field is id_user, as it is stated there, instead of "id" like on the users table.
I don't know how to proceed from here on out, since i am new to php.
Thanks
A simple JOIN can get this for you -
SELECT USERS.name
FROM USERS
JOIN DATA
ON USERS.name = DATA.name

Retrieving specific ID once

I have a table named users and has a user_id, and a table named groups and has a group_id and also have user_id that is a foreign key reference from users's user_id.The situation is here: if the user joined a group, his/her user_id is inserted into table groups. So if the user joined two different groups, the column 'user_id' in table 'groups' will insert two or more same user_id's. Well, I just want to bring the user_id once, either he/she joined two or more groups..
I have no idea how to loop it properly without getting user_id that is the same.... I just want it to loop once...
$query_groups = mysql_query("SELECT * FROM groups");
while ($rows_g = mysql_fetch_assoc($query_groups)) {
$g_user_id = $rows_g['user_id'];
$query_users = mysql_query("SELECT * FROM users WHERE user_id='$g_user_id'");
while ($rows_u = mysql_fetch_assoc($query_users)) {
echo $rows_u['user_id'];
}
}
change your code as follows:
$query_groups = mysql_query("SELECT user_id FROM groups LEFT JOIN users ON users.user_id = groups.user_id GROUP BY groups.user_id");
while($rows = mysql_fetch_assoc($query_groups))
{
echo $rows['user_id'];
}
You are using $rows_g but the variable is namend $rows in the first while loop.
Wrong:
$g_user_id = $rows_g['user_id'];
Correct:
$g_user_id = $rows['user_id'];
But try to use joining tables, because this is an inefficient way to get the wanted data.
In your case you should use LEFT JOIN.

Selecting user specific records for logged in user using multiple tables PHP MYSQL

I have 2 tables one called users and one called tv shows. Im storing the name of the user in a variable called username by doing. The users table holds the user_id PK, username, password and the tv shws table stores the tv_id PK, user_id FK, TV Show Name
$username=$_SESSION['username'];
i want to be able to display all the tv shows for the specific user that has logged in and im guessing i would need to show all the results for the user id assigned to the user that has logged in because the user_id in the tv shows table is a foreign key of the primary key user id in the users table.
Code:
$user = "SELECT user_id FROM users where username='$username'";
if(!$rs = mysql_query("SELECT * FROM tv shows WHERE user_id='$user'")) {
When i run this code i get "cannot select table"
Try this
$query = 'SELECT * FROM tv_shows where user_id=(SELECT user_id FROM users where username="'.$username.'")';
okay ,try this:
<?php
$user = musql_query("SELECT * FROM users where username='$username'");
$result = mysql_fetch_array($user);
$userid = $result['user_id'];
$sql = "SELECT * FROM tv shows WHERE user_id=".$userid;
$get_tv = mysql_query($sql);
$make_array = mysql_fetch_assoc($get_tv);
print_r($make_array);
?>
Happy coding!!
First of all, as a better design, you can have user_id in your $_SESSION.. so that you can avoid unnecessary query...
Here the problem could be due to Single quote ... So please escape your Single quote ..
Thanks
SELECT username,user_id
FROM users as a
JOIN tv shows as b ON b.user_id_id=a.user_id
WHERE a.username='{$username}'
managed to figure it out myself with help from Tornado
if(!$rs = mysql_query("$query2")) {
$query2 = 'SELECT * FROM tv_shows where user_id=(SELECT user_id FROM users where username="'.$username.'")';

Categories