How can i replace table name in this query string with another string or table name with php
SELECT
Panel.Id as PanelId,Panel.Title as PanelTitle,Panel.Icon as PanelIcon,
SubPanel.Id as SubPanelId,SubPanel.Title as SubPanelTitle,
SubPanel.Icon as SubPanelIcon,SecurityAccess.Id as Access,SecurityAccess.Controller,
SecurityAccess.Action
FROM Panel
INNER JOIN SubPanel
INNER JOIN SecurityAccess
WHERE
Panel.Id > 0 AND SubPanel.Panel = Panel.Id AND SubPanel.Id = UsersAccess.Subpanel
and SubPanel.Id > 0
ORDER BY Panel._Order,SubPanel._Order
For example: replace "Panel" with "my_panel"
I do not want you to change my String.
Just use this string as it exists.
I hope this helps you..
$sql = "SELECT
$table_1.Id as PanelId, $table_1.Title as PanelTitle, $table_1.Icon as PanelIcon,
$table_2.Id as SubPanelId, $table_2.Title as SubPanelTitle,
$table_2.Icon as SubPanelIcon, $table_3.Id as Access, $table_3.Controller,
$table_3.Action
FROM $table_1
INNER JOIN $table_2
INNER JOIN $table_3
WHERE
$table_1.Id > 0 AND $table_2.Panel = $table_1.Id AND $table_2.Id = UsersAccess.Subpanel
and $table_2.Id > 0
ORDER BY $table_1._Order, $table_2._Order";
for your current query
it would be like..
$table_1 = "Panel";
$table_2 = "SubPanel";
$table_3 = "SecurityAccess";
If $table_ vars are user input than be careful to escape them before putting into query.
Related
$result = mysqli_query($db, "SELECT * FROM 'category' JOIN 'post-item' ON category.category-id = post-item.category-id");
1 post-item can have 1 category and 1 category can have many post-item.
Change
SELECT * FROM 'category'
JOIN 'post-item' ON category.category-id = post-item.category-id"
to
SELECT * FROM `category`
JOIN `post-item` ON `category`.`category-id` = `post-item`.`category-id`
You have single quote which is not correct
You need to specify particular fields because with * it cannot recognize which fields to fetch and from which table.
So do it something like
"SELECT table1.field1, table1.field2, table2.field4 FROM category JOIN post-item ON category.category-id = post-item.category-id"
Also you can leave out ` or ' while querying through PHP except when you are supplying dynamic variable.
i'm in the process of joining two tables together under two different conditions. For primary example, lets say I have the following nested query:
$Query = $DB->prepare("SELECT ID, Name FROM modifications
WHERE TYPE =1 & WFAbility = '0'");
$Query->execute();
$Query->bind_result($Mod_ID,$Mod_Name);
and this query:
$Query= $DB->prepare("SELECT `ModID` from `wfabilities` WHERE `WFID`=?");
$Query->bind_param();
$Query->execute();
$Query->bind_result();
while ($Query->fetch()){ }
Basically, I want to select all the elements where type is equal to one and Ability is equal to 0, this is to be selected from the modifications table.
I further need to select all the IDs from wfabilities, but transform them into the names located in modifications where WFID is equal to the results from another query.
Here is my current semi-working code.
$Get_ID = $DB->prepare("SELECT ID FROM warframes WHERE Name=?");
$Get_ID->bind_param('s',$_GET['Frame']);
$Get_ID->execute();
$Get_ID->bind_result($FrameID);
$Get_ID->fetch();
$Get_ID->close();
echo $FrameID;
$WF_Abilties = $DB->prepare("SELECT ModID FROM `wfabilities` WHERE WFID=?");
$WF_Abilties->bind_param('i',$FrameID);
$WF_Abilties->execute();
$WF_Abilties->bind_result($ModID);
$Mod_IDArr = array();
while ($WF_Abilties->fetch()){
$Mod_IDArr[] = $ModID;
}
print_r($Mod_IDArr);
$Ability_Name = array();
foreach ($Mod_IDArr AS $AbilityMods){
$WF_AbName = $DB->prepare("SELECT `Name` FROM `modifications` WHERE ID=?");
$WF_AbName->bind_param('i',$AbilityMods);
$WF_AbName->execute();
$WF_AbName->bind_result($Mod_Name);
$WF_AbName->fetch();
$Ability_Name[] = $Mod_Name;
}
print_r($Ability_Name);
See below:
SELECT ModID,
ID,
Name
FROM modifications M
LEFT JOIN wfabilities WF
ON WF.ModID = M.ID
WHERE TYPE =1 & WFAbility = '0'
To do this, you need to join your tables, I'm not quite sure what you are trying to do so you might have to give me more info, but here is my guess.
SELECT ID, Name, ModID
FROM modifications
JOIN wfabilities
ON WFID = ID
WHERE TYPE = '1'
AND WFAbility = '0'
In this version I am connecting the tables when WFID is equal if ID. You will have to tell me exactly what is supposed to be hooking to what in your requirements.
To learn more about joins and what they do, check this page out: MySQL Join
Edit:
After looking at your larger structure, I can see that you can do this:
SELECT modifications.Name FROM modifications
JOIN wfabilities on wfabilities.ModID = modifications.ID
JOIN warframes on warframes.ID = wfabilities.WFID
WHERE warframes.Name = 'the name you want'
This query will get you an array of the ability_names from the warframes name.
This is the query:
"SELECT A.ID, A.Name,B.ModID,C.Name
FROM modifications as A
LEFT JOIN wfabilities as B ON A.ID = B.WFID
LEFT JOIN warframes as C ON C.ID = B.WFID
WHERE A.TYPE =1 AND A.WFAbility = '0' AND C.Name = ?"
I have updated an old system and I have a query problem.
To get to business this is the direct problem:
There are new records witch are associated with an id to other records. There are also old records which are not.
Case1 : there is a INT id and when that is present the query has to use AND after an include.
Case 2 : when the value of the INT id is 0 it has to do nothing
This is the part of the query where I need to make an variable AND statement:
LEFT JOIN table v ON v.producten_id = i.producten_id AND v.t5_id = i.t5_id AND i.id = v.inkoop_id
I never used IF stamens inside a query but i am looking for something like this:
LEFT JOIN table v ON v.producten_id = i.producten_id AND v.t5_id = i.t5_id if(v.inkoop_id > 0){AND i.id = v.inkoop_id}
Try this:
LEFT JOIN table v
ON v.producten_id = i.producten_id
AND v.t5_id = i.t5_id
AND (v.inkoop_id <= 0 OR i.id = v.inkoop_id)
How about this....
LEFT JOIN table v ON v.producten_id = i.producten_id
AND v.t5_id = i.t5_id
AND ((v.inkoop_id > 0 AND i.id = v.inkoop_id) OR (v.inkoop_id = 0))
Hey guys need some more help
I have 3 tables USERS, PROFILEINTERESTS and INTERESTS
profile interests has the two foreign keys which link users and interests, they are just done by ID.
I have this so far
$statement = "SELECT
InterestID
FROM
`ProfileInterests`
WHERE
userID = '$profile'";
Now I want it so that it selects from Interests where what it gets from that query is the result.
So say that gives out 3 numbers
1
3
4
I want it to search the Interests table where ID is = to those...I just don't know how to physically write it in PHP...
Please help.
Using a JOIN:
Best option if you need values from the PROFILEINTERESTS table.
SELECT DISTINCT i.*
FROM INTERESTS i
JOIN PROFILEINTERESTS pi ON pi.interests_id = i.interests_id
WHERE pi.userid = $profileid
Using EXISTS:
SELECT i.*
FROM INTERESTS i
WHERE EXISTS (SELECT NULL
FROM PROFILEINTERESTS pi
WHERE pi.interests_id = i.interests_id
AND pi.userid = $profileid)
Using IN:
SELECT i.*
FROM INTERESTS i
WHERE i.interests_id IN (SELECT pi.interests_id
FROM PROFILEINTERESTS pi
WHERE pi.userid = $profileid)
You are on the right track, lets say you execute the query above using this PHP code:
$statement = mysql_query("SELECT InterestID FROM `ProfileInterests`
WHERE userID = '$profile'");
Then you can use a PHP loop to dynamically generate an SQL statement that will pull the desired IDs from a second table. So, for example, continuing the code above:
$SQL = "";
while ($statementLoop = mysql_fetch_assoc($statement)) {
//Note the extra space on the end of the query
$SQL .= "`id` = '{$statementLoop['InterestID']}' OR ";
}
//Trim the " OR " off the end of the query
$SQL = rtrim($SQL, " OR ");
//Now run the dynamic SQL, using the query generated above
$query = mysql_query("SELECT * FROM `table2` WHERE {$SQL}")
I haven't tested the code, but it should work. So, this code will generate SQL like this:
SELECT * FROM `table2` WHERE `id` = '1' OR `id` = '3' OR `id` = '4'
Hope that helps,
spryno724
Most likely you want to join the tables
select
i.Name
from
ProfileInterests p
inner join
interests i
on
p.interestid = i.interestid
where
p.userid = 1
ok here is my php and mysql code:
where it is bold i wanted to the the uid from the online table and if it in there
where online.uid = '' i needed so it put the uid in there.
$sql = ("select accounts.id,
accounts.tgid,
accounts.lastactivity,
cometchat_status.message,
cometchat_status.status,
**online.uid**
from friends_list join accounts
on friends_list.fid = accounts.id
left join cometchat_status
on accounts.id = cometchat_status.userid
where friends_list.status = '1'
and **online.uid = ''**
and friends_list.uid = '".mysql_real_escape_string($userid)."'
order by tgid asc");
#sledge identifies the problem in his comment above (I'm not sure why he didn't post an answer).
You are selecting a column from the online table, but you don't include it in your FROM clause. You have to query from a table in order to reference its columns in other parts of the query. For example:
$sql = ("select accounts.id,
accounts.tgid,
accounts.lastactivity,
cometchat_status.message,
cometchat_status.status,
online.uid
from friends_list
join accounts on friends_list.fid = accounts.id
join online on ( ??? )
left join cometchat_status
on accounts.id = cometchat_status.userid
where friends_list.status = '1'
and online.uid = ''
and friends_list.uid = '".mysql_real_escape_string($userid)."'
order by tgid asc");
You need to fill in the join condition, because there's not enough information in your original post to infer how the online table is related to other tables.
PS: Kudos for using mysql_real_escape_string().