I know this question is very common on Stack, but I can't seem to find an answer or a snippet of code that actually solves my problem...
I have two tables, accounts and orders. I want to write a SQL statement to pull Order ID, Date, Total and Status from orders, and also Username from accounts.
The statement would be something like this:
$ohsql = "select * from orders where Username = '". $login_session ."'";
Obviously Username will come from the accounts table, but the principal is there. I am missing the join as I am clueless about it!
You need to 'link' the two tables. For that do something like :
- add a column accountid to Orders table; so this tells us, which order belongs to which user . We then use this info in our JOIN.
Easy way to do it in 2 queries :
// get the id value of the username
$id = select id from accounts table where username = $login_session
// use that in the JOIN
select * from orders JOIN accounts ON orders.accountid = accounts.id where accounts.id = $id
Assuming that your orders table contains the accountId and your accounts table containing the user name use following query
$ohsql = "select o.*, a.username from orders o
INNER JOIN accounts a ON a.id = o.accountId
WHERE a.username = '". $login_session ."'";
Let me know if you face any issue
Related
users table
mandates table
I would like to select data in my queries only the list that is based on the user name currently logged in. I tried to use this query selection but it's still selecting all data that can be searched in the table. I would like the "u.name" is gonna be equal to the username in my other table.
<?php
$query = "SELECT m.*,u.name FROM mandates m, users u WHERE m.provider = u.name";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run)>0)
{
foreach($query_run as $mandates)
{
you need to do a join
select u.name, m.* from users u
left join mandates m on (u.name = m.provider)
where u.name = (pass the username of whoever is logged in)
although i would suggest linking mandates to users by user ID instead of just the persons name in case of name changes etc.
There are two signup tables one for hire and one for work pictures of the both tables are given below:
Table 1:
Table 2
but i want to create single login form for both hire and work user
now i wrote this query for extracting data from both tables,
$sql = " SELECT *
FROM signup_hire
LEFT OUTER JOIN signup_work ON signup_hire.id = signup_work.id
UNION
SELECT *
FROM signup_hire
RIGHT OUTER JOIN signup_work ON signup_hire.id = signup_work.id
WHERE signup_hire.email = '$myusername' and signup_hire.password = '$mypassword'
OR signup_work.email = '$myusername' and signup_work.password = '$mypassword'";
but I am receiving 'invalid email or password' - what is the correct query to use?
Have you tried a simple UNION? Query the first table and add a UNION with the same result on the second table, this will will return the results if it matches one of the tables, keep in mind that column names will the ones on the first table regardless which one matches. Also, be sure both tables have the same number of columns or list the fields you need instead of *
SELECT id,name,email
FROM signup_hire
WHERE signup_hire.email = '$myusername'
AND signup_hire.password = '$mypassword'
UNION
SELECT id,name,email
FROM signup_work
WHERE signup_work.email = '$myusername'
AND signup_work.password = '$mypassword';
I want to apply conditions with my query like i want to interchange one column's value to another column. Here is my query and now i am not getting how to apply
mysqli_query($connect, "
select
user_pro.User_No,
user_pro.Fist_Name,
user_pro.Last_Name,
user_pro.Designation,
user_pro.Profile,
user_pro.ProfileDP,
user_fndrst.Frnd_SNo,
user_fndrst.Rqst_Sender,
user_fndrst.Rqst_Receiver,
user_fndrst.Rqst_Status
from
user_profile INNER JOIN user_fndrst ON user_pro.User_No = user_fndrst.Rqst_Sender
WHERE
user_fndrst.Rqst_Status='1' AND
user_fndrst.Rqst_Receiver='$user_id'
ORDER BY Frnd_SNo DESC
");
Here "user_pro" table contains user's details, "user_fndrst" table contains user's friend request status and $user_id is user's logined id. Here I want that IF user_fndrst.Rqst_Receiver='$user_id' THEN user_fndrst.Rqst_Receiver value change to user_fndrst.Rqst_Sender.
For this I have stuck with user friend request status fetching from table "user_fndrst".
It seems your query has some problems. Use this:
SELECT
user_pro.User_No, user_pro.Fist_Name, user_pro.Last_Name, user_pro.Designation, user_pro.Profile, user_pro.ProfileDP, user_fndrst.Frnd_SNo, user_fndrst.Rqst_Sender, user_fndrst.Rqst_Receiver, user_fndrst.Rqst_Status
FROM
user_profile user_pro
INNER JOIN user_fndrst ON user_pro.User_No = user_fndrst.Rqst_Sender
WHERE
user_fndrst.Rqst_Status='1' AND user_fndrst.Rqst_Receiver='$user_id'
ORDER BY
user_fndrst.Frnd_SNo DESC
Hi I am creating an array of user id's with a query. With another query I would like to select from a given table where the user_id is one that is in the array created from my very first query. How can I use an array in my WHERE clause?
Just for reference: $row_interest is the array
My Code:
//Grabs the user id's of the users that have the queried interest
$interest_search_query= "SELECT DISTINCT user_id FROM interests WHERE interest LIKE
'%".$search_term."%'";
$interest_search_result= mysqli_query($connect, $interest_search_query);
$row_interest= mysqli_fetch_array($interest_search_result);
//Grabs the user information with each user id
$search_query= "SELECT DISTINCT user_id, fname, lname, profile_pic, school FROM users
WHERE user_id IN $row_interest";
I tried "WHERE user_id IN $row_interest", but it doesn't seem to work. What could I be doing wrong?
Thanks.
$search_query= "SELECT DISTINCT user_id, fname, lname, profile_pic, school FROM users
WHERE user_id IN (".implode(',',$row_interest).")";
You can actually merge both queries.
SELECT distinct user_id, fname, lname, profile_pic, school
FROM users
WHERE user_id in
(SELECT distinct user_id from interests
where interest like %{search_term}%)
You could build an IN() clause for your SQL in PHP, but since the set is coming from another query you could use a JOIN to do this.
Edit:
I can't test this without your data, but the join would be something like
$search_query= "SELECT DISTINCT u.user_id, u.fname, u.lname, u.profile_pic, u.school
FROM user u
INNER JOIN interests i ON u.user_id = i.user_id
WHERE i.interest like '%".$search_term."%'";
It sounds like you could just use a join, but $row_interest is an array, and it is interpolated as "Array" in the query. It seems like you want to build the entire array first
$rows = array();
while ($row = mysqli_fetch_array($interest_search_result)) {
$rows[] = $row['user_id'];
}
Then you can create the "IN" clause you need.
"WHERE user ID IN (" . implode(",", $rows) . ")"
Your code is vulnerable to injection. You should properly parameterize the queries using prepared statements. This is more difficult to do with a variable number of arguments in mysqli as I understand it, but it is something to keep in mind.
Im trying to find an efficient way to display all posts from people who are being followed by the logged in account holder.
There are two key tables:
1- Posts
table name : posts
id, account_name, published, body
2- Follows
Table name : follows
id, account_name, followed_name
I'm trying to find a way that i can display all the posts from all the accounts that are being followed. The connection between Posts and Follows is the Account_name.
I understand that it will probably be a join, but it's how I construct the WHERE clause. So far I have the following (The account name is set via $_SESSION['account_name']):
$sql = "SELECT * FROM posts LEFT JOIN follows ON posts.account_name = follows.account_name WHERE --- How would I only get the posts from the accounts being followed ?---"
I'm sure this is something simple my brain just feels drained and I cant seem to work it out.
UPDATE Attempting in PDO
Returning NULL at the moment,
$sql = "SELECT * FROM share_posts WHERE account_name IN (SELECT followed_name FROM $this->account_follows WHERE account_name = :account_name)";
return $this->AC->Database->select($sql, array('account_name' => $account_name));
The goes to my Database Class:
public function select($sql, $array = array(), $fetch_mode = PDO::FETCH_ASSOC)
{
$stmt = $this->AC->PDO->prepare($sql);
foreach ($array as $key => $value)
{
$stmt->bindValue("$key", $value);
}
$stmt->execute();
return $stmt->fetchALL($fetch_mode);
}
The returned data is NULL at the moment even though the logged in account has followed other accounts.
$account = $_SESSION['account_name'];
//do some sql injection checking on $account here
$sql = "SELECT * FROM posts WHERE account_name IN (SELECT followed_name FROM follows WHERE account_name='".$account."')";
This will get all the posts where the account name matches somebody you follow. I wasnt sure who was following who, but in this case the followed_name are the people account_name is following. If thats the other way around, switch the values
$sql = "SELECT * FROM posts WHERE account_name IN (SELECT account_name FROM follows WHERE followed_name='".$account."')";
I will write this the way I interpret your question.
What you need to do is select only the posts from the users that are followed by your logged in user.
To break this down, first you want to select the users followed by the logged in user. To do this, we use the Follows table.
We then want to select the posts by these users. As such my query would be this.
SELECT posts.* FROM follows
LEFT JOIN posts ON posts.account_name = follows.follows_name
WHERE follows.account_name = $logged_in_user