How to create a contacts page properly? - php

I want to do a contacts page. Here is my table structure:
Accounts Table:
1) id
2) User_id
3) Full_name
4) Username
5) Email
6) Password
Contacts Table:
1) My_id (This is the user who added friend_id)
2) Contact_id (this is the contact who was added by my_id)
3) Status
My query looks something like this:
$sid = ID of user who is viewing;
$sql = "SELECT STRAIGHT_JOIN DISTINCT contacts.contact_id,
accounts.full_name FROM contacts INNER JOIN accounts
on contacts.contact_id = accounts.user_id WHERE
contacts.my_id = '$sid' OR contacts.contact_id = '$sid'";
The problem is that it doesn't work the right way. I end up seeing my name in the query (meaning when i login i see my name in the contacts instead of contacts name).
How to fix this? Thanks.

The STRAIGHT_JOIN keyword should not be necessary here. To get the contact information, use a second JOIN to the contacts table which relates it back to the accounts.
SELECT
DISTINCT c.contact_id,
cn.full_name
FROM
accounts a
/* first join connects the adding user's id to records in contacts */
INNER JOIN contacts c ON a.user_id = c.My_id
/* second join connects contact list user ids back to names in accounts */
INNER JOIN accounts cn ON cn.user_id = c.Contact_id
WHERE a.User_id = '$sid'

This query ought to suffice:
SELECT DISTINCT contacts.contact_id, accounts.full_name
FROM contacts, accounts
WHERE (contacts.my_id = '$sid' AND contacts.contact_id = accounts.user_id)
OR (contacts.contact_id = '$sid' AND contacts.my_id = accounts.user_id)

Related

How to get the most recent entry from a database table for a certain user

I have a table that displays information on users in a database:
$query = "SELECT reviews2_users.id AS IDUSER,
signature,
user,
email,
address,
COUNT(reviews2_reviews.id) AS COUNTER
FROM reviews2_users LEFT JOIN reviews2_reviews ON (reviews2_users.id = reviews2_reviews.id_user)
GROUP BY reviews2_users.id
ORDER BY COUNTER DESC";
For every user in the table I'd like to show the date of the LATEST entry that they've made into the database.
I tried this but it doesn't work (and I'm not very good with databse/mysql):
$query = "SELECT reviews2_users.id AS IDUSER,
signature,
user,
email,
address,
COUNT(reviews2_reviews.id) AS COUNTER
FROM reviews2_users LEFT JOIN reviews2_reviews ON (reviews2_users.id = reviews2_reviews.id_user)
AND LEFT JOIN reviews2_reviews ON max(reviews2_reviews.date)
GROUP BY reviews2_users.id
ORDER BY COUNTER DESC";
Could somebody help me out on this?
Thank you
Please add MAX(date) to first query:
$query = "
SELECT reviews2_users.id AS IDUSER
, signature
, user
, email
, address
, COUNT(reviews2_reviews.id) AS COUNTER
, MAX(reviews2_reviews.date) as date
FROM reviews2_users
LEFT
JOIN reviews2_reviews
ON (reviews2_users.id = reviews2_reviews.id_user)
GROUP
BY reviews2_users.id
ORDER
BY COUNTER DESC
";

Inner join of two tables [UPDATED]

tbl teacher
lrn
fname
lname
email
image
schedule
tbl user
lrn
fname
lname
email
image
account-type
$id=$_SESSION['id'];
$get_record_sched = mysqli_query ($db, "SELECT schedule FROM teacher INNER JOIN users ON teacher.lrn = users.lrn WHERE teacher.lrn = '$id' ");
I want to echo the exact schedule based on the id of the logged in account. But with this, query, there's no output...
You said you have $id from the session. Assuming that column lrn is the ID, then
Select t.schedule
From teacher t
Where t.lrn = $id
No need to join the table with the user table.
Note that parameter binding is advised.
$statement = $connection->prepare("Select t.schedule From teacher t Where t.lrn = ?");
$statement->bind_param("s", $id);
In you where condition was wrong.They don't have any param to validate condition.so change like this
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id WHERE 1 "
OR
use same where condition with join
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id AND teacher.schedule = users.id WHERE 1 "
OR
if You have logged used id in session like
$id=$_SESSION['user_id'];
"SELECT schedule FROM teacher INNER JOIN users ON teacher.id = users.id WHERE teacher.schedule='$id' "

Join two table from MySQL database

I need to join table accounts and table users from my MySQL database.
$sql = "SELECT id FROM `".DB_ACCOUNTS."` WHERE `id` IS NOT NULL ";
table accounts and table users contain a same column as id. I want to get gender, birthday from users and bodybuild, weight, height, ethnictype, skincolor from accounts.
Users Table
Accounts table:
How to do this? Please help.
SELECT US.gender, US.birthday,AC.bodybuild, AC.weight, AC.height, AC.ethnictype, AC.skincolor
FROM accounts AC
INNER JOIN users US ON AC.ID=US.ID
You can try above code.
It will helps you.
$sql = "SELECT u.gender, u.birthday, a.bodybuild, a.weight, a.height, a.ethnictype, a.skincolor
FROM accounts a
LEFT JOIN users u on a.id = u.id
WHERE a.id IS NOT NULL ";
please try above query...
Change id column in User table with userid.after that you must add userid at Accounts table as Foreign Key. userid column at Account table contain userid value from User table.
ALTER TABLE Accounts
ADD FOREIGN KEY (userid) REFERENCES Users(userid);
sql = "SELECT u.gender,u.birthday,ac.bodybuild, ac.weight, ac.height, ac.ethnictype, ac.skincolor
FROM Users u
LEFT JOIN Accounts ac
ON u.userid=ac.userid
WHERE ac.userid IS NOT NULL; ";

How to display users from two MySQL tables

I have two mysql tables-members and addclique. Members table contains the details of all users while addclique table is 'friends' table with two columns-adder_id, which contains id of the user that sent a friend request and clique_id, contains the id of user that accepted the friend request.
Please am trying to select the friends of a particular user but am finding hard getting the details (photo, name, etc) of the friends from the members table because I don't know at which point to join members table to the addclique table.
$sql = "SELECT i.*, m.* FROM addclique i JOIN members m ON m.id = ******** WHERE adder_id = :id OR clique_id = :id";
$result= $db->query($sql, array('id' => $_SESSION['id']);
The query below will select all members that a particular person has added plus all members that have added the same person.
select m.* from members m
join addclique a on m.id = a.adder_id
where a.clique_id = :id
union
select m.* from members m
join addclique a on m.id = a.clique_id
where a.adder_id = :id
If I'm understanding your question correctly, you need to join on m.id = a.adder_id or m.id = a.clique_id. You can do this with in:
set #id := 4;
select *
from members m
join addclique a on m.id in (a.adder_id, a.clique_id)
where #id in (a.adder_id, a.clique_id)
and m.id != #id
SQL Fiddle Demo

MySQL SELECT * FROM tbl1 WHERE smth AND a field from another table is something

I have two tables. One contains information on hotels, added to my website by users. It contains a field called Username that contains the username of the person that has uploaded it. The second table contains user info (including a field called "active" which indicates if a user has paid or not). I need to get entries from the first table ONLY for users that have paid. Currently I'm doing it like this in PHP:
$hotelsq = mysql_query("SELECT * FROM trips_all ORDER BY id DESC ");
while ($hotel = mysql_fetch_array($hotelsq)) {
$username = $hotel['username'];
$isactiq = mysql_query("SELECT * FROM users WHERE username='$username'");
$isact = mysql_fetch_array($isactiq);
if ($isact['active'] == 'member') {
What I need is a single query that will select all of the hotels in the first table, then check if the user ("username" column) it has been uploaded from has "member" in the "active" column. The username column in the second table is named "username" as well.
What would that query look like ?
select t.*
from trips_all t
inner join users on t.username = u.username
where u.active = 'member'
Check INNER JOIN:
SQL INNER JOIN Keyword
So something along the lines of:
mysql_query("SELECT * FROM trips_all INNER JOIN users ON trips_all.username=users.username WHERE users.active='member');
select * from trips_all t
left join users u on t.id= u.userid
where u.active = 'member'

Categories