How to apply conditions in mysqli query - php

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

Related

Check First on Table A, if not found Check on other table

I am creating a Log in and I have separate tables for Users A and Users B.
What I want to do is check first in first table if the Users that trying to Login is in the Table A,
if YES, it will not go to the Table B to check the Login credentials, if NOT, go to Table B and check the Login credentials.
Table A
SELECT * FROM tableA WHERE userId='$userId' AND password='$password'
Table B
SELECT * FROM tableB WHERE accountNumber='$accountNumber' AND password='$password'
Note: The 2 Tables has different Field Name userId and accountNumber.
I presume you are fetching the values of username and password from client side so I will tell you only what you asked for.
$getUserBasic1=$db->prepare('SELECT * FROM tableA WHERE userId="$userId" AND password="$password"');
$getUserBasic1->execute();
$user= $getUserBasic1->fetchAll();
if(count($user)>0)
{
//if yes do what you want here
}
else
{
$getUserBasic2=$db2->prepare('SELECT * FROM tableB WHERE accountNumber="$accountNumber" AND password="$password"');
$getUserBasic2->execute();
$user2= $getUserBasic2->fetchAll();
//write your code here
}
You could use an INNER JOIN and select both table results taking Table A's result first if it exists, else take Table B's result.
Assuming both tables have some sort of reference like the User ID you can use something like this:
SELECT tbla.*, tblb.* FROM tableA tbla
INNER JOIN tableB tblb ON tbla.userId = tblb.userId
WHERE userId='$userId' OR accountNumber='$accountNumber' AND password='$password'
ORDER BY userId ASC
LIMIT 1
The query above uses the cross-reference (userId in this case) and joins both tables together before querying the results. It orders the results by Table A before Table B but limits the result to 1 bringing either Table A or Table B out depending which is null.
Try combining the tables, some thing like:
SELECT * FROM tableA, tableB WHERE tableA.userId='$userId' AND tableA.password='$password' OR tableB.accountNumber='$accountNumber' AND tableB.password='$password'
I have not checked, so may not work, but see if this gets what you are looking for!
Something like this:
$sql = "SQL QUERY FOR TABLEA";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// checking if result in TABLE A
}
else{
//search in TABLE B by updating your sql value.
}
I hope that you want to check for the registered user, the best way to do that is to keep one table and just search there itself keeping the userID as the primary key.

Holiday Home - Displaying the owner's who is signed in properties

$queryread = "SELECT Properties.ID, Properties.Name,
Properties.Description, County.County
FROM Properties
INNER JOIN County ON Properties.County_ID = County.ID
AND Properties.Ban = 0";
This is the query to display properties on the public website. When an owner is signed in how would i display only the properties that the owner owns.
Owner table is called "RegisteredOwners" and id is "id", there is also a "Owner_ID" in the Properties table.
Thanks
You will simply need to add a WHERE clause to your query. To keep the answer simple and contained, I'll just use a subquery for the where clause. The query below is identical to yours, with an added line for the where clause.
SELECT Properties.ID, Properties.Name, Properties.Description, County.County
FROM Properties INNER JOIN County ON (Properties.County_ID = County.ID AND Properties.Ban = 0)
WHERE Properties.Owner_ID = (SELECT id FROM RegisteredOwners WHERE Name=?)
You will need to replace the Name=? portion with the appropriate method of identifying the user's RegisteredOwners row.

MySQL JOIN pass PHP variable to two tables

I have the following MySQL query in PHP that passes a variable to complete the query:
SELECT * from mobile_tech WHERE uid=$uid order by timestamp DESC limit 0,1
I have the following MySQL JOIN that provides data from two tables:
SELECT mobile_tech.latitude, mobile_tech.longitude, mobile_tech.timestamp, mobile_tech.uid, gbl_qemplisting.EmpNo, gbl_qemplisting.FirstName, gbl_qemplisting.LastName
FROM mobile_tech, gbl_qemplisting
WHERE mobile_tech.uid=gbl_qemplisting.EmpNo AND date(timestamp)='$currentday'
group by uid
I need to combine these two queries into one with a JOIN and still passing the $uid variable to complete the query.
I've tried the following and it did not work:
SELECT mobile_tech.latitude, mobile_tech.longitude, mobile_tech.timestamp, mobile_tech.uid, gbl_qemplisting.EmpNo, gbl_qemplisting.FirstName, gbl_qemplisting.LastName
FROM mobile_tech, gbl_qemplisting
WHERE mobile_tech.uid=$uid AND gbl_qemplisting.EmpNo=$uid AND date(timestamp)='$currentday'
Your query will return a cross product between the mobile_tech and gbl_emplisting rows for $uid. If you just want one row, as in the first query, use ORDER BY and LIMIT similarly.
SELECT mobile_tech.latitude, mobile_tech.longitude, mobile_tech.timestamp, mobile_tech.uid, gbl_qemplisting.EmpNo, gbl_qemplisting.FirstName, gbl_qemplisting.LastName
FROM mobile_tech, gbl_qemplisting
WHERE mobile_tech.uid=$uid AND gbl_qemplisting.EmpNo=$uid AND date(timestamp)='$currentday'
ORDER BY mobile_tech.timestamp
LIMIT 1
Please try with this and say the result
SELECT mobile_tech.latitude,
mobile_tech.longitude,
mobile_tech.timestamp,
mobile_tech.uid,
gbl_qemplisting.EmpNo,
gbl_qemplisting.FirstName,
gbl_qemplisting.LastName
FROM mobile_tech inner join gbl_qemplisting
on mobile_tech.uid=gbl_qemplisting.EmpNo
where mobile_tech.uid=$uid AND date(timestamp)='$currentday'

How to Display data from 2 different Database

I have an hard time to retrieve data in a table from MySQL database.
I have 2 different database that cannot be merge but there is a table in the first database that is identical to the second database.
Description Database 1 table: areas : ar_id, name, password.
Description Database 2 table: user : id, username, pass.
Now, When the user Login, He logs in the 2nd database. in each page of the user I have use $_SESSION['username'] to call the username.
Importantly, In every page, I have table that displays data from different tables using the username in the 2 Database; this else the SQL to be specific and only provide each user with their own information. and That's Ok. This is the SQL:
SELECT Client_table.Name, Client_table.Client_Id FROM Client_table, user WHERE user.username = '" . $_SESSION['username'] . "' AND Client_table.Branch = user.area Order by Name ASC
In one of the page, I totally using the 1st Database with this SQL to display data in the table :
select site_id, site_name from sites WHERE srep_id = 5
AND status = 1 or status = 2
order by site_name asc
QUESTION: I would like to display this SQL data in a table by using the username or id from the 2nd database BUT is returns Empty Table (I include both Database in this page). This is my current SQL but still not displaying anything:
SELECT cl.client_name, st.site_id, st.site_name
FROM Database1.sites st
JOIN Database2.user u ON u.id = st.ar_id
JOIN Database1.clients cl ON cl.client_id = st.client_id
WHERE Database1.st.name = '".$_SESSION['username']."'
AND st.status > 0
ORDER BY st.site_name ASC
NOTE: This is a major problem that took me almost a week!
Please some one help!
I think I have an answer.
After browsing and doing some search, I sound that I can make use of the $_SESSION here and Also, This was my final SQL Statement that Helped me to Connect the 2 Database from the same SQL Statement by using variable in PHP Script.
session_start();
$result = mysql_query("SELECT cl.client_name, st.site_id, st.site_name, ar.rep_id
FROM sites st
JOIN areas ar ON ar.rep_id = st.srep_id
JOIN clients cl ON cl.client_id = st.client_id
WHERE st.srep_id = '".$_SESSION['userarea']."'
AND st.status > 0
ORDER BY st.site_name ASC");

php mysql where IN clause

I have a simple query:
$user_id = $_SESSION['user_id'];
SELECT * FROM pages WHERE user_id IN($user_id);
field user_id in pages tables has the following format 1,3,5,...
it contains multiple user_id
What I need is to select all rows based on logged in user_id.
The above attempt does not works, it only picks up the first number.
You need to use find_in_set():
select *
from pages
where find_in_set(user_id, $user_id) > 0;
Alternatively, you can construct the SQL so it has the values in the string. Something like:
select *
from pages
where user_id in (".$user_id.")"

Categories