I am currently trying to display the username of people who i am following, the problem is that during the following process, only the ID of me and the person i'm following is stored.
I've got it to the point where the ID's are displayed but i'd like to show the names hyperlinked. $p_id is the profile ID.
Here's what I've got:
$following = mysql_query("SELECT `follower`, `followed` FROM user_follow WHERE follower=$p_id");
I am following: <?php while($apple = mysql_fetch_array($following)){
echo '+'.$apple['followed'].' ';
}?>
The usernames are in a different table "users" under the field "username" - I need them to match up with the ID's that are currently displayed, and be displayed.
So what you need is a pair of JOINs (implicitly INNER JOIN) against users, one to join in the follower and one to join in the followed.
SELECT
/* Rather than SELECT * you need to be specific about the columns, and
give them aliases like followed_name since you have 2 tables with the same
column names in the query */
ufollower.id AS follower_id,
ufollower.username AS follower_name,
ufollowed.id AS followed_id,
ufollowed.username AS followed_name
FROM
/* JOIN twice against users, once to get the follower and once to get the followed */
user_follow
/* users aliased as ufollower to get the follower details */
JOIN users ufollower ON ufollower.id = user_follow.follower
/* users aliased as ufollowed to get the followed details */
JOIN users ufollowed ON ufollowed.id = user_follow.followed
WHERE
user_follow.follower = $p_id
In your loop, the names are available in follower_name, followed_name.
while($apple = mysql_fetch_array($following)){
// Be sure to wrap the name in htmlspecialchars() to encode characters that could break html.
// This has the followed id in the href and the followed name in the link text...
echo '+'.htmlspecialchars($apple['followed_name']) .' ';
}
You need to join them together - and because you want to output two id's names independently, you have to double-join the same table twice.
SELECT
uf.follower,
uf.username AS followerUsername,
uf.followed,
u2.username AS followedUsername
FROM
user_follow AS uf
INNER JOIN
users AS u1
ON
uf.follower = u1.userID
INNER JOIN
users AS u2
ON
uf.followed = u2.userID
WHERE
follower = ?
Please use prepared statements or escaping if you create SQL statements! A call to mysql_real_escape_string() saves you from SQL injection attacks.
Related
I am trying to only show unique userIds (userIds are (0,1,2,3,4,5,6,7,8,9 etc...) for the query I am running. I tried using DISTINCT in my query, but it only shows me unique values of the rows that have 2 or more of the same userId.
Is there a way I can use php to only show the unique values. My weak points are arrays and it makes it more complicated because its using data from a MySQLi query.
Example right now I have with the query now (lets say its GROUP BY rentPaid DESC and the rent total is 800.00 for all users):
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
3--------400.00------April
1--------200.00------April
1--------100.00------April
Example desired output:
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
Can I do this with MYSQL because I tried DISTINCT and it wouldn't work, how about PHP?
Query:
SELECT
properties.*,
leases.*,
users.userId, users.primaryPhone,
CONCAT(users.userFirstName,' ',users.userLastName) AS user,
admins.adminName, payments.*
FROM
properties
LEFT JOIN leases ON properties.propertyId = leases.propertyId
LEFT JOIN assigned ON properties.propertyId = assigned.propertyId
LEFT JOIN admins ON assigned.adminId = admins.adminId
LEFT JOIN users ON properties.propertyId = users.propertyId
LEFT JOIN payments ON properties.propertyId = payments.propertyId
WHERE
payments.rentMonth = '$currentMonth' AND
payments.rentYear = '$currentYear'
Edit: Please excuse my formatting, this is my first post.
Edit: Added query....its long, but works lol. I only want unique userIds (no double or triple userIds etc...)
I suspect this is what you want:
SELECT userID, MAX(rentPaid) AS maxRentPaid, rentMonth
FROM yourTable
WHERE rentMonth = "April"
GROUP BY userID
ORDER BY maxRentPaid
I'm making a search engine in which a page visitor can search for music artists based on 4 different attributes which the artists will have a rating of from 0- 100 and by entering the minimum value of a specified attribute, the visitor can view the list of artists with ratings greater than or equal to the desired value. After the query I have the fetch array and foreach statement already set but I am having trouble with the query.
I've tried the following query statement. It's one cohesive statement:
SELECT users.username, databaseimage.profile
// users.username is artists username
// databaseimage is table where profile pic is stored
FROM users
JOIN databaseimage ON users.id = databaseimage.user_id
JOIN attributes ON users.id = attributes.userid
// user.id, database.user_id, and attribute.userid all correspond to the id of a specified artist
// attributes is table where attributes are stored
The above gets me all the data that I need. Below is the part I need help with. I want to narrow the data down such that only the data corresponding to artists with attribute ratings (as attr) greater than $selectnumber (Number specified by the visitor) is in the the result array. This what I have tried.
WHERE attribute.userid
HAVING COUNT() IN (
SELECT userid, ($attribute DIV TotalRatingEntries) as attr
FROM attributes
WHERE attr >= '$selectnumber'
)
A far as I understand data structure I can propose the query below:
SELECT
users.username,
databaseimage.profile
FROM
users
JOIN databaseimage ON users.id = databaseimage.user_id
JOIN attributes ON users.id = attributes.userid
WHERE
attribute.userid IN
(
SELECT
userid
FROM
attributes
group by
userid,TotalRatingEntries
having
sum(attribute)/TotalRatingEntries >= '$selectnumber'
)
Please provide more details about these 3 tables, maybe I misunderstood something.
I have tables posts and users
basically i want to query if the column privacy of posts table is 0 it means post is public and i want to show this to everyone. but if privacy column is 1,2,3,4 it means this post is only for users with ID 1, 2, 3 and 4
This is not a conditional problem it's a sql join through table association problem.
You need to create a join table that associates a post to a user. You can then use a join to select posts based upon their assignment to a user.
The SQL might look something like this
To select posts assigned to user ID = 4
SELECT * FROM `posts`
LEFT JOIN `posts_users` ON (`posts`.`id` = `posts_users`.`post_id`)
WHERE `posts_users`.`user_id` = 4;
You have also made the assumption that if a post has no users assigned to it, then it's a public post. That requires a check for NULL associations.
SELECT * FROM `posts`
LEFT JOIN `posts_users` ON (`posts`.`id` = `posts_users`.`post_id`)
WHERE `posts_users`.`user_id` IS NULL;
If you want to find both public and associated posts, then you use a logical OR to match both rules.
SELECT * FROM `posts`
LEFT JOIN `posts_users` ON (`posts`.`id` = `posts_users`.`post_id`)
WHERE `posts_users`.`user_id` = 4
OR `posts_users`.`user_id` IS NULL;
I have 2 table
Table-1 - user
user(ID.Name,Class)
Table-2 - Category
Category(ID,user_id,cat_id)
if user input a data from text field the how to search data from both table
Just query both tables and use the 'OR' operator for the columns you want to search in
SELECT * from user, category WHERE user.id=[text field] or category.user_id=[text field] or category.cat_id=[text field]
PHP Example: (assuming you are using MySQL database - you also need mysqli enabled in your php.ini file)
$mysqli = mysqli_connect(HOSTNAME, USERNAME, PASSWORD, DATABASE);
if (mysqli_connect_errno($mysqli)) {throw new exception("Failed to connect to MySQL: " . mysqli_connect_error());}
$sql = " SELECT * from user, category WHERE user.id='".$text_field."' or category.user_id='".$text_field."' or category.cat_id='".$text_field."'";
$rows = $result->fetch_array(MYSQLI_ASSOC);
foreach($rows as $row){
print_r($row);
}
This should get the records and show you the response from the array.
I hope you have added primary key & foreign key relations to these tables, in-order to grab data from both the tables you have two ways, either do multiplication of both table and bring in all the data else make use of JOIN that does the same in efficient way
Hoping to have your table schema as
USER [table] having id, username, name, password
CATEGORY [table] having id, name, description, user_id
So the query will become
SELECT U.*, C.id as cat_id, C.name as cat_name, C.description as cat_desc
FROM USER U
JOIN CATEGORY C ON C.user_id = U.id
If you have user inputting data from input fields, that becomes a filter query to be added in our above query, assume user is entering Name of category and wants the result set of the same then the above query gets added with WHERE clause as below
WHERE C.name LIKE '%{INPUT FIELD CONTENT HERE}%'
I have used LIKE clause above to allow us doing PARTIAL search
Hope this helps you
I need to pull data from two tables, and it's a bit over my head :
The first tables contains a list of members (member_id, username, email ...)
The second table stores relations between members (id, member_id, friend_id)
When a member adds another member as a friend, both member_ids are stored in the second table.
Now I need to output that second table, I'd like to output usernames instead of numbers :
example :
{username corresponding to member_id} added {username corresponding to friend_id} as a friend
Can someone help with the query ?
You need to perform a double join on members
SELECT mem1.username, mem2.username
FROM members mem1
INNER JOIN relations
ON mem1.member_id = relations.member_id
INNER JOIN member mem2
ON relations.friend_id = mem2.member_id
Something like:
select tb1.username as member_name,
tb2.username as friend_name
from membertable as tb1
inner join
(
membertable as tb2,
memberrelationstable
)
on
(
tb1.member_id = memberrelationstable.member_id and
tb2.member_id = memberrelationstable.friend_id
)
This is how I would do it:
SELECT member.username AS member_username, friend.username AS friend_username
FROM relations
INNER JOIN members AS member
ON relations.member_id = member.member_id
INNER JOIN members AS friend
ON relations.friend_id = mem2.member_id
I've spaced it so that you can easily see how we're joining the members table twice, and simple giving it a different name both times.
Whenever something is followed by AS, it means that you're giving it another name. This allows you to use the same table multiple times in a single query.