I have two tables that link together through an id one is "submit_moderate" and one is "submit_post"
The "submit_moderate" table looks like this
id moderated_by post
1 James 60
2 Alice 32
3 Tim 18
4 Michael 60
Im using a simple query to get data from the "submit_post" table according to the "submit_moderate" table.
$get_posts = mysql_query("SELECT * FROM submit_moderate WHERE moderated_by!='$user'");
$user is the person who is signed in.
Now my problem is when I run this query, with the user 'Michael' it will retrieve this
1 James 60
2 Alice 32
3 Tim 18
Now technically this is correct however I don't want to retrieve the first row because 60 is associated with Michael as well as James. Basically I don't want to retrieve that value '60'.
I know why this is happening however I can't figure out how to do this. I appreciate any hints or advice I can get.
SELECT DISTINCT post
FROM submit_moderate
WHERE post NOT IN (SELECT post
FROM submit_moderate
WHERE moderated_by = 'Michael')
PS: not sure, but in some cases it probably would worth changing select-part of the nested query to SELECT DISTINCT post
Suppose $user = 'Michael';
If u really don't want the 1st row of user as you said that james matches with the michael in posts
then go for
$post = mysql_query_first("SELECT post FROM submit_moderate WHERE moderated_by='$user'");
$get_posts = mysql_query("SELECT * FROM submit_moderate WHERE moderated_by!='$user' and post!='$post');
or
$get_posts = mysql_query("SELECT * FROM submit_moderate WHERE post NOT IN (SELECT post FROM submit_moderate WHERE moderated_by = 'Michael') and moderated_by != 'Michael' ");
Related
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.
I have Data base "players" with:
id Name gold
1 joe 50
2 tom 40
3 jzd 70
I use a PHP to get info from base to variable:
$base = $connection->query("SELECT * FROM Players");
$data = $base->fetch_assoc();
When i use
$data['name'];
I get only a name FIRST id, how to get name for example id 2 or 3?
If you want to return the record for a specific user use
SELECT * FROM players WHERE id = <id>;
If you want to list all the players returned from your first query (select all, no where statement), you need to loop through the array returned from doing your query. Use a foreach (here are some examples
Basically, I have a database with 10 exam types. Each type has two parts, and is updated as pass or fail. I need to list the total count of exams that have not been completed (both parts passed).
I've tried this and it returns the count if either part shows pass, not both.
$query = sprintf(
"SELECT * FROM candidate_exams
WHERE gID='1' AND canID='%d' AND exResult='y'
GROUP BY gEID",
(int) $canID
);
$result = $con->query($query);
$rowCount = 10 - mysqli_num_rows($result);
'gID' is an identifier that tracks what group these 10 exams come from,
'canID' is a candidate identifier,
'gEID' is an exam type.
SELECT COUNT(*) FROM tabele WHERE type_a = 'fail' OR type_b = 'fail'
something like this?
It would help a lot to see your table structure to be able to answer this question properly.
I have tried upto end everything to the best of my knowledge but all in vain. I'm building a community website and i am stuck at one thing. I want to display a text on my wall when my friends become friends with other users. Just like Facebook displays it. For eg.
"John doe is now friends with max stone"
And
"John doe is now friends with max stone and 5 other people"
I am successfully getting it displayed but not like the one I showed above. I do it using while loop from notifications table. I tried but couldn't get it like facebook shows. so please help me get this thing done.
I am posting my code so that you can get clear idea of my code and my mistakes in my code so you can clear.
$ check_if_friendship_created = mysql_query ("SELECT * FROM `users_notifications`
WHERE `friend_1_username` IN (SELECT `friend_2_username` FROM `users_friends`
WHERE `friend_1_username` = '". $ logged_user ['username']."')
GROUP BY `friend_2_fullname` ORDER BY `notification_time` DESC");
also to let you all know that my friends table is a symmetrical in design...
looking forward to your positive reponse. Thank you....
why don't you have a separate table for friendship notification? Meaning when user1 is friend with user2 you insert the details to the friendship notification table.
table structure
id reqeust_sent_by request_accepted_by friendship_date
So let's assume John sends friend request and then Jack accepts the request, when Jack accepts the request then you insert that detail to the table. You better use mysqli prepared statements or pdo. So here is mysqli prepared statements
$request_sender = 'John';
$logged_user = 'Jack';
$date = date('Y-m-d H:i:s');
$mydabtase = new mysqli('localhost', 'root', '', 'database_name');
$stmt = $mydatabse->prepare("insert into friendship_notification (request_sent_by, request_accepted_by, friendship_date) values (?,?,?)");
$stmt->bind_param('sss' $request_sender, $logged_user, $date);
$stmt->execute();
$stmt->close();
now to select the data you can do
$stmt = $mydatabase->prepare("select * from friendship_notification where request_sent_by ! = ? or requst_accepted_by ! = ? order by id desc");//do your select here. here we are selecting where either rows are different from the logged in user because we don't want to show the logged in user that he has became friends with somebody else, we show this for other users.
$stmt->bind_param('ss', $logged_user, $logged_user);//follow the same procedure when binding the parameters, s means string. if you have 2 ? then you need 2 s along with 2 variables.
$stmt->execute();
$result = $stmt->get_result();//this gets the results
$total = $result->num_rows;//this returns the total number of rows for the above select query
while($row = $result->fetch_assoc()){
//if the total is less than 3 people we do the below
if($total < 3){
echo $row['request_accepted_by']."is now friends with".$row['request_sent_by'].",";
}
elseif($total > 3 ){
$stmt2 = $mydatabase->prepare("select * from friendship_notification where request_sent_by ! = ? or requst_accepted_by ! = ? order by id desc limit 1");//do your select here
$stmt2->bind_param('ss', $logged_user, $logged_user);
$stmt2->execute();
$result2 = $stmt2->get_result();
$row2 = $result2->fetch_array();//we only need one row so no need for while loop.
echo $row['request_accepted_by']."is now friends with".$row2['request_sent_by']."and ".$total-1." others.";
}
}
So this will display something like
John is now friends with Max and magna. and on the second case
John is now friends with Max and 4 others.
if you want the facebook way then you need to use ajax to auto refresh.
Currently my DB looks similar too...
ID Username Interest
04 Tommy Soccer
04 Tommy Internet
32 Jack Soccer
32 Jack Swimming
32 Jack Boxing
I have a page on my website where the user can specify his/her interests and depending on what they enter, those with the same interests will be displayed.
So If Tommy was to visit my page and add "Boxing" as an Interest Jack would show up as he has "Boxing" listed within the table.
I need to write a query to do this but i'm unsure of the best way to do it as i'm still very new to PHP, would something along the lines of...
mysql_query(SELECT * FROM interests_table WHERE Interest = $interest1 || $interest2 || Interest3);
mysql_query("SELECT * FROM interests_table WHERE (Interest = $interest1 OR Interest = $interest2 OR Interest = $Interest3)");
Even shorter:
mysql_query("SELECT * FROM interests_table WHERE Interest IN ($interest1, $interest2, $interest3)");
Of course you will need to add necessary parameters escaping for more secure code.
If you have all possible values you want to search for in array, the code may look like this:
$interests = array('boxing', 'swimming', 'speedway');
// ... query preparation
$result = mysql_query("SELECT * FROM interests_table WHERE Interest IN (" . implode('", "', mysql_real_escape_string($interests)) . ")");
// rest of the code ...
But this code is only effective for small sets of data (few to hundreds). If you'd have more interests on list, you should find more effective way.