Getting multiple rows of the same thing from database - php

I'm having some issues with this mysql database i got from school. I'm getting duplicate rows with the same name but different quantities. I would suppose that this is because 'inventory' contains columns with the same name as in 'sets'. But i really can't get it to work. Thank you in advance for all your help.
Database schematic (in Swedish, but the titles are in English.):
http://weber.itn.liu.se/~stegu76/TNMK30-2016/legodatabasen.pdf
I'm very new to this so any tips would be appreciated.
$setidquery = "SELECT inventory.Quantity, inventory.ItemID, inventory.ColorID, colors.Colorname, parts.Partname, sets.SetID, sets.Year, inventory.ItemtypeID FROM `inventory`, `parts`, `colors`, `sets` WHERE sets.SetID='$_COOKIE[setid]' AND inventory.ItemID=parts.PartID AND inventory.ColorID=colors.ColorID ORDER BY Partname ASC LIMIT 1000";
echo "<table class=\"table\">";
echo "<tr><th>Quantity:</th><th>Year:</th><th>Partname:</th><th>ItemID:</th><th>SetID</th><th>Image:</th></tr>";
while($row = mysqli_fetch_array($result)){
$prefix = "http://www.itn.liu.se/~stegu76/img.bricklink.com/";
$Year = $row['Year'];
$Quantity = $row['Quantity'];
$ItemID = $row['ItemID'];
$ColorID = $row['ColorID'];
$Partname = $row['Partname'];
$SetID = $row['SetID'];
$ItemtypeID = $row['ItemtypeID'];
$imagesearch = mysqli_query($conn, "SELECT * FROM `images` WHERE ItemTypeID = '$ItemtypeID' AND ItemID = '$ItemID' AND ColorID = '$ColorID' ");
$imageinfo = mysqli_fetch_array($imagesearch);
if($imageinfo['has_jpg']) {
$filename = "$ItemtypeID/$ColorID/$ItemID.jpg";
} else if($imageinfo['has_gif']) {
$filename = "$ItemtypeID/$ColorID/$ItemID.gif";
} else {
$filename = "noimage_small.png";
}
echo "<tr>
<td>$Year</td>
<td>$Quantity</td>
<td>$Partname</td>
<td>$ItemID</td>
<td>$SetID</td>
<td><img src=\"$prefix$filename\" alt=\"Part $ItemID\"/></td>
</tr>";
}
echo "</table>";
Here is the query formatted for readability:
SELECT inventory.Quantity,
inventory.ItemID,
inventory.ColorID,
colors.Colorname,
parts.Partname,
sets.SetID, sets.Year,
inventory.ItemtypeID
FROM `inventory`, `parts`, `colors`, `sets`
WHERE sets.SetID='$_COOKIE[setid]'
AND inventory.ItemID=parts.PartID
AND inventory.ColorID=colors.ColorID
ORDER BY Partname ASC LIMIT 1000;

The problem is that inventory, parts and colors are not joined to sets.
It is OK to do the joins in the where clause, but, as Strawberry says we prefer to write the joins in the from clause now. I think that makes them easier to read, which helps to spot missing joins.
Here is your select statement (formatted to help me see what is going on):
select
inventory.quantity,
inventory.itemid,
inventory.colorid,
colors.colorname,
parts.partname,
sets.setid,
sets.year,
inventory.itemtypeid
from
inventory,
parts,
colors,
sets
where
sets.setid='$_cookie[setid]' and
inventory.itemid = parts.partid and
inventory.colorid = colors.colorid
order by
partname asc
limit 1000;
And here is the join version. I've shown the missing join with "?" where you need to add something:
select
inventory.quantity,
inventory.itemid,
inventory.colorid,
colors.colorname,
parts.partname,
sets.setid,
sets.year,
inventory.itemtypeid
from
inventory
join
parts
on inventory.itemid = parts.partid
join
colors,
on inventory.colorid = colors.colorid
join
sets
on set.? = ?.?
where
sets.setid='$_cookie[setid]'
order by
partname asc
limit 1000;
The important thing is that there is no join to sets. I would guess that there are three sets (for each of the examples shown anyway). Because you have not said how to join to sets the query returns the other rows once for each set, in other words it joins to every set because you haven't restricted the join in any way.
If you want to learn more about SQL there are many options. My website is www.thedatastudio.net.

Related

Counting the number of times each variable appears in table

Basically, I am seeking to know if there is a better way to accomplish this specific task.
Basically, what happens is I query the db for a list of "project needs" -- These are each uniquer and only appear once.
Then, I search another table to find out how many members have the required "skills - which are directly correlated to the project needs".
I accomplished exactly what I was trying to do by running a second query and then inserting them into an array like this:
function countEachSkill(){
$return = array();
$query = "SELECT DISTINCT SKILL_ID, SKILL_NAME FROM PROJECT_NEEDS";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
$query = "SELECT COUNT(*) as COUNT FROM MEMBER_SKILLS WHERE SKILL_ID = '".$row['NEED_ID']."'";
$cResult = mysql_query($query);
$cRow = mysql_fetch_assoc($cResult);
$return[$row['SKILL_ID']]['Count'] = $cRow['COUNT'];
$return[$row['SKILL_ID']]['Name'] = $row['SKILL_NAME'];
}
arsort($return);
return $return;
}
But I feel like there has to be a better way (perhaps using some kind of join?) that would return this in a result set to avoid using the array.
Thanks in advance.
PS. I know mysql_ is depreciated. It is not my choice on which to use.
SELECT P.SKILL_ID, P.SKILL_NAME, COUNT(M.SKILL_ID) as COUNT FROM PROJECT_NEEDS P INNER JOIN MEMBER_SKILLS M
ON P.SKILL_ID=M.SKILL_ID
GROUP BY P.SKILL_ID, P.SKILL_NAME
I've adjusted Nriddens answer to accomodate for the select distinct, Im under the belief that his adjustment would be ok given SKILL_ID is a primary key
function countEachSkill(){
$return = array();
$query = "
SELECT
COUNT(*) AS COUNT,
PROJECT_NEEDS.SKILL_NAME,
PROJECT_NEEDS.SKILL_ID
FROM
(SELECT DISTINCT
SKILL_ID, SKILL_NAME
FROM
PROJECT_NEEDS) AS PROJECT_NEEDS
INNER JOIN
MEMBER_SKILLS
ON
MEMBER_SKILLS.SKILL_ID = PROJECT_NEEDS.SKILL_ID
GROUP BY PROJECT_NEEDS.SKILL_ID";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
$return[$row['SKILL_ID']]['Count'] = $row['COUNT'];
$return[$row['SKILL_ID']]['Name'] = $row['SKILL_NAME'];
}
arsort($return);
return $return;
I am subquerying on the select distinct because I dont believe you have a dedicated skills table with an auto inc primary key, if that was there I wouldn't be using a subquery.
Can you test this query
select project_needs.*,count(members_skills.*) as count from project_needs
inner join members_skills
on members_skills.skill_id=project_needs.skill_id Group by project_needs.skill_name, project_needs.skill_id

total sum of a row

I have this query which gives me the transactions for a user, and the output is a table with the information. There is this row named basket_value which contains some numbers, and I need to get the sum of those numbers. Could you please help me?
$query3 = 'SELECT users.first_name,users.last_name,users.phone,
retailer.date, SUM(retailer.basket_value),
retailer.time,retailer.location,retailer.type_of_payment
FROM users ,retailer
WHERE users.user_id="'.$id_user.'"
AND users.user_id=retailer.user_id GROUP BY users.user_id';
$result3 = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result3)) {
echo "Total ". $row['user_id']. " = $". $row['SUM(basket_value)'];
echo "<br />";
}
I suppose that also your query have some problem and suppose that you have an id to retrieve users, I would do in that way
$query3 = 'SELECT users.user_id,users.first_name,users.last_name,
users.phone, retailer.date,
SUM(retailer.basket_value) as total_sum,
retailer.time,retailer.location,retailer.type_of_payment
FROM users ,retailer WHERE users.user_id="'.$id_user.'"
AND users.user_id=retailer.user_id
GROUP BY users.user_id,users.first_name,users.last_name,
users.phone, retailer.date,retailer.time,retailer.location,
retailer.type_of_payment '
$result = $mysqli->query($query3);
Now if you want the sum for each user:
while ($row = $result->fetch_row()) {
echo "Player: ".$row['user_id']." total: ".$row['total_sum'];
}
If you want the WHOLE GLOBAL sum, you have to way:
Modify your query in that way:
SELECT SUM(retailer.basket_value) as total_sum
FROM retailer
Sum into while loop like: $total += $row['total_sum'];
You're missing a GROUP BY on your query. You most likely want to add GROUP BY users.user_id
Try this query:
SELECT u.first_name, u.last_name, u.phone, SUM(r.basket_value) as total_sum
FROM users u
JOIN retailers r
ON u.user_id = r.user_id
WHERE u.user_id="'.$id_user.'"
GROUP BY u.user_id
You can omit all other columns in the select list and only have the sum() aggregate run on the set to avoid the GROUP BY clause.
$query3 = 'SELECT SUM(retailer.basket_value) as total_sum
FROM users ,retailer WHERE users.user_id="'.$id_user.'"
AND users.user_id=retailer.user_id';

Removing redundant SQL queries (in PHP) by using JOIN

I am trying to clean up my SQL queries and use JOIN in just ONE where I once used TWO queries.
Here is former code (in PHP):
$cat = "books"; // as a test
$query = "SELECT category, cat_id FROM master_cat WHERE category = '{$cat}'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$cat_id = $row['cat_id'];
}
$sql = mysql_query("SELECT item, spam, cat_id FROM items WHERE cat_id = ' " . $cat_id . "' ORDER BY TRIM(LEADING 'The ' FROM item) ASC;");
while ($row = mysql_fetch_assoc($sql))
if ($row['spam'] < 2)
$output[] = $row;
print(json_encode($output)); // added!
I am trying to just remove the top query and use a JOIN. The updated SQL statement is this:
EDIT: I made a mistake in original question. Basically user input gives us $cat = "something". There is "something" in master_cat table with a cat_id. That cat_id is also in the items table. That is where I need the tables to connect -- and the WHERE clause needs to incorporate "$cat"
UPDATED QUERY:
$result = mysql_query("SELECT i.item, i.spam, mc.cat_id AS Category
FROM items as i
INNER JOIN master_cat as mc
ON i.cat_id = mc.cat_id
WHERE i.cat_id = '{$cat}'
ORDER BY TRIM(LEADING 'The ' FROM i.item) ASC;");
then:
while ($row = mysql_fetch_assoc($result))
if ($row['spam'] < 2)
$output[] = $row;
I receive this in the browser:
null.
Can someone guide me on how to properly use JOIN which I know will REALLY clean things up here and make more efficient coding. I just watched a tutorials but still am not quite getting it.
HERE IS FINAL CODE THAT WORKS
$cat = $_POST['category']; // yes, yes, injection. this is just the short version.
$result = mysql_query("SELECT i.item, i.cat_id, i.spam, mc.cat_id, mc.category, TRIM(LEADING 'The ' FROM i.item) as CleanItem
FROM items as i
INNER JOIN master_cat as mc
ON i.cat_id = mc.cat_id
WHERE mc.category = '{$cat}'
ORDER BY CleanItem ASC;");
while ($row = mysql_fetch_assoc($result))
if ($row['spam'] < 2)
$output[] = $row;
You have reference the items table by its full name in the WHERE clause, you should be using the alias you created (i).
You also have an ambiguous column reference item in your ORDER BY clause.
Try changing the last two lines to:
WHERE i.cat_id = '{$cat_id}'
ORDER BY TRIM(LEADING 'The ' FROM i.item) ASC
You should also inspect mysql_error() to get a string description of the error, which would have pointed you straight to the problem.
$sql = "SELECT i.item, i.spam, mc.cat_id AS Category
FROM items as i
INNER JOIN master_cat as mc
ON i.cat_id = mc.category
WHERE items.cat_id = '{$cat_id}'
ORDER BY TRIM(LEADING 'The ' FROM item) ASC";
Since you aliased it you have to keep it as aliased: fix the second to last line:
WHERE i.cat_id = '{$cat_id}'
Try this one:
$sql = "SELECT i.item, i.spam, mc.cat_id AS Category
FROM items as i
INNER JOIN master_cat as mc ON i.cat_id = mc.cat_id
WHERE i.cat_id = '{$cat_id}'
ORDER BY TRIM(LEADING 'The ' FROM item) ASC";
If you alias items as i, you should use i everywhere else. Besides, mc.category does not seem to exist so I replaced it with mc.cat_id. What does mysql_error say?
You are probably getting false as your response to the initial query and not a result set
You should join on i.cat_id = mc.cat_id
You should also probably perform your i.item cleaning (i.e. removing 'The ') in the select statement (even as a seperate field if you need to keep i.item intact) and then order by that field.
You should reference i.cat_id = '{$cat_id]}', not items.cat_id

PHP MYSQL query - Strip away columns from response

Is it possible to strip away columns from the response I get in a query where I join 3 tables and need more or less all columns for the query itself so that some columns aren't visible in the response?
This is the query I have:
$sth = mysql_query("
SELECT
tbl_subApp2Tag.*,
tbl_subApp.*,
tbl_tag.*
ISNULL(tbl_userDeviceNOTTag.userDevice_id) AS selected
FROM tbl_subApp2Tag
LEFT JOIN tbl_subApp
ON tbl_subApp.id = tbl_subApp2Tag.subApp_id
AND tbl_subApp.subApp_id = '".$sub."'
LEFT JOIN tbl_tag
ON tbl_tag.id = tbl_subApp2Tag.tag_id
LEFT JOIN tbl_userDeviceNOTTag
ON tbl_userDeviceNOTTag.tag_id = tbl_tag.id
AND tbl_userDeviceNOTTag.userDevice_id = '".$user."'
WHERE tbl_subApp2Tag.subApp_id = tbl_subApp.id
ORDER BY tbl_tag.name ASC ");
if(!$sth) echo "Error in query: ".mysql_error();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
You do not need to include columns in the result table, just because they are referenced elsewhere in the query. Just select the columns that you need.

add value to result from mysql query that will be JSON encoded in PHP?

I would like to add a value to each row that I get from my query depending on if a row exist in another table. Is there a smart way to achieve this?
This is the code I have:
$sth = mysql_query("SELECT tbl_subApp2Tag.*, tbl_tag.* FROM tbl_subApp2Tag LEFT JOIN tbl_tag ON tbl_subApp2Tag.tag_id = tbl_tag.id WHERE tbl_subApp2Tag.subApp_id = '".$sub."' ORDER BY tbl_tag.name ASC");
if(!$sth) echo "Error in query: ".mysql_error();
while($r = mysql_fetch_assoc($sth)) {
$query = "SELECT * FROM tbl_userDevice2Tag WHERE tag_id='".$r['id']."' AND userDevice_id='".$user."'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result)) {
$r['relation'] = true;
$rows[] = $r; //Add 'relation' => true to this row
} else {
$r['relation'] = false;
$rows[] = $r; //Add 'relation' => false to this row
}
}
print json_encode($rows);
Where the //Add ... is, is where I would like to insert the extra value. Any suggestions of how I can do this?
I'm still a beginner in PHP so if there are anything else that I have missed please tell me.
EDIT: Second query was from the wrong table. This is the correct one.
Edited Edited below query to reflect new information because I don't like leaving things half-done.
$sth = mysql_query("
SELECT
tbl_subApp2Tag.*,
tbl_tag.*,
ISNULL(tbl_userDevice2Tag.userDevice_id) AS relation
FROM tbl_subApp2Tag
LEFT JOIN tbl_tag
ON tbl_tag.id = tbl_subApp2Tag.tag_id
LEFT JOIN tbl_userDevice2Tag
ON tbl_userDevice2Tag.tag_id = tbl_tag.id
AND tbl_userDevice2Tag.userDevice_id = '".$user."'
WHERE tbl_subApp2Tag.subApp_id = '".$sub."'
ORDER BY tbl_tag.name ASC
");
Though the above feels like the LEFT JOIN on tbl_tag is the wrong way around, but it's hard to tell as you are vague on your eventual aim. For example, if I was to assume the following
Tags will always exist
subApp2Tag will always exist
You want to know if a record in tbl_userDevice2Tag matches the above
Then I would do the following instead. The INNER JOIN means that it won't worry about records in tbl_tag that are not on the requested subApp_id which in turn will limit the other joins.
$sth = mysql_query("
SELECT
tbl_subApp2Tag.*,
tbl_tag.*,
ISNULL(tbl_userDevice2Tag.userDevice_id) AS relation
FROM tbl_tag
INNER JOIN tbl_subApp2Tag
ON tbl_subApp2Tag.tag_id = tbl_tag.id
AND tbl_subApp2Tag.subApp_id = '".$sub."'
LEFT JOIN tbl_userDevice2Tag
ON tbl_userDevice2Tag.tag_id = tbl_tag.id
AND tbl_userDevice2Tag.userDevice_id = '".$user."'
ORDER BY tbl_tag.name ASC
");
you have to do all the job in a single query.
Why can't you just $r['append'] = "value"; before adding $r to the array?

Categories