Get the names of IDs from different table - php

I have an array with a lot of data from a database. In the array, there are two fields called teameinsid and teamzweiid. The query is a very simple one :
$spiel = mysql_query("select teameinsid, teamzweiid from begegnung", $connection) or die("Keine passende Begegnung");
What I need is to search in the database for the names of these IDs. The names are in a different table.
What I have now is the following:
while($tmp = mysql_fetch_array($spiel)){
$teins = $tmp['teameinsid'];
$tzwei = $tmp['teamzweiid'];
}
So I know the two IDs, but I don't know where to save the names. If I try:
$name = mysql_query("select name from team where teameinsid = $teins", $con)
it gets overwritten every time. How can I manage this?
EDIT:
Database Scheme:
Table Team : id, name
Table Begegnung: id, teameinsid, teamzweiid

I tested this, and it works if your query in $speil is this:
SELECT B.*, T1.name AS teamnameeins, T2.name as teamnamezwei FROM begegnung AS B
JOIN team AS T1 ON T1.id = B.teameinsid
JOIN team AS T2 ON T2.id = B.teamzweiid
http://dev.mysql.com/doc/refman/5.0/en/join.html
You should use your code now like this:
while($tmp = mysql_fetch_array($spiel)){
$teins = $tmp['teamnameeins'];
$tzwei = $tmp['teamnamezwei'];
}

change your code like this
while($tmp = mysql_fetch_array($spiel)){
$teins[] = $tmp['teameinsid'];
$tzwei[] = $tmp['teamzweiid'];
}
now
$ids=implode ( ',' , $teins);
$name = mysql_query("select name from team where teameinsid in($ids)", $con)

Related

PHP Query MySQL Tables from IDs from other tables

I have a database with several tables. I'm able to query IDs from a single table. What I'd like to do is Use those IDs to query another tables IDs, then use these new IDs to query fields from the final table. Here is what I am currently doing:
Here is how I acquire the first set of IDs:
$returnedPost = mysqli_query($con, "SELECT Region_ID FROM Region WHERE RegionName='" . $queryVar . "'");
function resultToArray($result) {
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
$rows = resultToArray($returnedPost);
//$rows[x]['Region_ID'];//returns Region_ID 1...n
I'd like to use the IDs in $rows to be able to query a new set of IDs from other tables as follows:
$newTbl = mysqli_query($con, "SELECT Location_ID FROM Location WHERE Region_ID=" . $rows[$x]['Region_ID']);
$rows2 = resultToArray($newTbl);
$finalTbl = mysqli_query($con, "SELECT Field1, Field2 FROM Posts WHERE Location_ID=" . $rows2[$x]['Location_ID']);
Can someone please tell me how I can accomplish this? Thanks.
you can use INNER JOIN in one query to get at this data, maybe something like this
SELECT P.Field1,P.Field2
FROM Region R
INNER JOIN Location L ON R.Region_ID = L.Region_ID
INNER JOIN Posts P ON L.Location_ID = P.Location_ID
WHERE R.RegionName = Your_Region_QueryVar

Display data from multiple tables and group them by a value (PHP/MySQL)

I have a couple of tables that i feel different data inside based on user inputs, the tables contain different columns, so i want to create like a page something like user activity to show him/her everything what he was doing, now i have a couple of tables that hold data and i need somehow to select all the data based on one account number that all the tables contain it and display to the user based on the date, what i had until now is below:
global $wpdb;
global $userInfo;
$il_wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
This example with 2 tables only, but i have around 6 tables that i need to join them
$_IL_TABLE_NAME = $wpdb->prefix . "il_complaints";
$_IL_TABLE_NAME1 = $wpdb->prefix . "il_internal_transfer";
$_IL_GET_COOKIES_VAL = $userInfo['account'];
//my query
$_IL_QUERY_RESULT = $il_wpdb->get_results( "SELECT $_IL_TABLE_NAME.il_date, $_IL_TABLE_NAME1.il_amount FROM $_IL_TABLE_NAME, $_IL_TABLE_NAME1 WHERE $_IL_TABLE_NAME.il_mt4_account='$_IL_GET_COOKIES_VAL' ");
//display data
foreach($_IL_QUERY_RESULT as $_IL_ROW){
echo $_IL_ROW->il_date;
echo $_IL_ROW->il_amount;
}
Because data will be displaed here from multiple tables how i can put to them something first raw is coming from first table and put to it table 1, the second row is coming from second table and to put to it table 2, to show to the user what he/she did and from where
I will appreciate any help.
try based on comments
$_IL_QUERY_RESULT = $il_wpdb->get_results( "
SELECT pr_il_complaints.il_date, pr_il_internal_transfer.il_amount
FROM pr_il_complaints t1 INNER JOIN pr_il_internal_transfer t2 ON t1.il_from_mt4 = t2.il_mt4_account;
");
You need a SQL JOIN, like this:
SELECT t1.name, t2.salary
FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;
SELECT t1.name, t2.salary
FROM employee t1 INNER JOIN info t2 ON t1.name = t2.name;
More: http://dev.mysql.com/doc/refman/5.0/en/join.html
try this...
$mysqli = new mysqli("localhost", "username", "password", "database");
$strr = "SELECT * FROM table1
INNER JOIN tabel2
ON table2.account_number = '".$something."'
INNER JOIN tabel3
ON table3.account_number = '".$something."'
";
$result = $mysqli->query($strr);
while($arr = $result->fetch_array())
{
echo "<pre>"; print_r($arr); echo "</pre>";
}
You need to use joins
SELECT TN.il_date, TN2.il_amount , TNX.field...
FROM $_IL_TABLE_NAME TN
JOIN $_IL_TABLE_NAME1 T2 ON TN.ID_FIELD = TN2.ID_FIELD
JOIN $_IL_TABLE_NAMEX TX ON TX.ID_FIELD = DATABASE.FIELD
JOIN ...
WHERE $_IL_TABLE_NAME.il_mt4_account='$_IL_GET_COOKIES_VAL'
Extra Information:
1.-Sintaxis de JOIN
2.-Understanding JOINs in MySQL and Other Relational Databases

Inner/Left join with two different where clauses

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 = ?"

MySQL use inner join in PHP

I have 2 tables, the unique id's in each table are the same in both tables.
How do I go about joining the data from both tables together in php?
When I normally pull data I do it like this:
$get_board_array = mysql_query("SELECT * FROM posts WHERE user_id_to = '$id' ");
while($posts = mysql_fetch_array($get_board_array))
{
$post_id = $get_post['post_id'];
$html_output .= "<p>".$post_id."</p>";
}
As for pulling the data from separate tables and not getting it all mixed up, I was thinking of doing something like this:
$get_arrayA = mysql_query("SELECT * FROM tableA WHERE age = '36' ");
while($dataA = mysql_fetch_array($get_arrayA))
{
$dataA_id = $dataA['id'];
$dataA_firstName = $dataA['FirstName'];
foreach($dataA_id)
{
$get_arrayB = mysql_query("SELECT * FROM tableB where id='".$dataA_id."'");
while($dataB = mysql_fetch_array($get_arrayB))
{
$dataB_lastName = $dataB['LastName'];
$html_output .= "<p>".$dataA_firstName.$dataB_lastName"</p>";
echo $html_output;
}
}
}
Or would this be just too weird of a thing?
I know how to do it within SQL using inner join but how do I do something like that in PHP and output html?
It is because of you misguide use of wildcard (*) characters. STOP THAT !! This is what happens when arrogant php developers imagine that they do not need to learn SQL. If you need something , then select it.
SELECT
foo.foo_id,
foo.data as paramX ,
bar.type,
bar.something_else
FROM foo
INNER JOIN bar USING (foo_id)
WHERE
foo.state = 'open'
AND bar.type = 3
This (I'm just guessing the schema and your needs) could be a better query for your situation:
SELECT a.id,a.FirstName,b.LastName
FROM tableA a
JOIN tableB b ON b.id = a.id
WHERE a.age = '36'

How Can I Query 2 Tables And Multiple Columns?

I researched this a bit already and all my attempts have come up short so far. I am trying to perform a mysql query in my php script that deals with multiple tables.
Here is what the tables look like:
TABLE 1
name
TABLE 2
Product (name)
Inventory
CatID
ProductID
TABLE 3
product_url
"name" (Table 1) must be the sane as "Product" (Table 2). Next, "Inventory" (table 2) must be = to "Y". Lastly, "CatID" must be = "2".
My attempt looked somewhat like this:
SELECT 1.name, 2.Product, 2.Inventory, 2.CatID
FROM table1 1, table2 2
WHERE 2.Inventory = 'Y'
AND 1.name = 2.Product
AND 2.CatID = '2'
From the results, I would be looking to get more information from the table such as product description, etc which would be in table1 and table2... I have never joined or queried 2 (or more) tables before. Any help would be greatly appreciated.
Try this:
SELECT table1.Name, table2.Product, tabl2.Inventory, table2.CatID
FROM table1 INNER JOIN table2
ON table1.Name = table2.Product
WHERE table2.CatID = '2'
SELECT t1.name, t2.Product, t2.Inventory, t2.CatID, t2.ProductID
FROM table1 t1
INNER JOIN table2 t2 ON t2.Product = t1.name
WHERE t2.Inventory = 'Y' AND t2.CatID = 2
I'm sorry to say that the database you have to work with was very poorly designed. If the query I gave you doesn't work, then make sure you have data in the tables that actually meets the criteria you're looking for.
Also remember that when you're accessing these fields in PHP, capitalization matters. You need to do something like this:
<?php
$q = QUERY FROM ABOVE
$r = mysql_query($q);
while($row = mysql_fetch_assoc($r)) {
$name = $row["name"];
$product = $row["Product"];
$inventory = $row["Inventory"];
$catid = $row["CatID"];
$productid = $row["ProductID"];
}
?>

Categories