Calculate Current Inventory from Three Arrays of Values - php

Sudipta's suggestion worked. Now I am having difficulty getting the final script to display online. The script displays properly in phpMyAdmin SQL editor.
$sql = "CREATE TABLE Inv_Physical_Count
SELECT SUM(qty) as qty, excel_part_num, part_id, part_desc, order_form_seq
FROM Inventory, Inventory_Items, Parts
WHERE Inventory.id = Inventory_Items.inventory_id
AND Inventory_Items.part_id = Parts.id
AND Inventory.date = '2017-01-05'
AND Inventory.inv_type_id = '2'
GROUP BY part_id
ORDER BY part_id";
$q = $pdo->prepare($sql);
$q->execute(array());
$sql = "CREATE TABLE Inv_Restock
SELECT SUM(qty) as qty, excel_part_num, part_id, part_desc, order_form_seq
FROM Inventory, Inventory_Items, Parts
WHERE Inventory.id = Inventory_Items.inventory_id
AND Inventory_Items.part_id = Parts.id
AND Inventory.date >= '2017-01-05'
AND Inventory.date < '2017-07-04'
AND Inventory.inv_type_id = '1'
GROUP BY part_id
ORDER BY part_id";
$q = $pdo->prepare($sql);
$q->execute(array());
$sql = "CREATE TABLE Inv_Orders
SELECT SUM(qty) as qty, excel_part_num, part_id, part_desc, order_form_seq
FROM Orders, Order_Items, Parts
WHERE Orders.id = Order_Items.orders_id
AND Order_Items.part_id = Parts.id
AND Orders.date_order >= '2017-01-05'
AND Orders.date_order < '2017-07-04'
GROUP BY part_id
ORDER BY part_id";
$q = $pdo->prepare($sql);
$q->execute(array());
The following SQL script works in SQL Editor in phpMyAdmin. However, in PHP I cannot get this script to display real content. It is all blank.
$sql = "SELECT a.qty + b.qty - c.qty as 'QTY', a.excel_part_num as 'Part Num', a.part_desc as 'Description'
FROM Inv_Physical_Count a,
Inv_Restock b,
Inv_Orders c
WHERE a.part_id = b.part_id
AND a.part_id = c.part_id
ORDER BY a.order_form_seq";
$q = $pdo->prepare($sql);
$q->execute(array());
while ($row = $q->fetch(PDO::FETCH_ASSOC))
{
echo '<tr>';
echo '<td>' . $row['qty'] . '</td>';
echo '<td>' . $row['excel_part_num'] . '</td>';
echo '<td>' . $row['part_desc'] . '</td>';
}

Can you not combine all your queries as per below. It will work, if you have the same part_id, if its not same, maybe you can join the tables with part_num and not part_id
SELECT SUM(a.qty + b.qty - c.qty), part_num
FROM Phys_Count a
LEFT JOIN Items_Received b
ON a.part_id = b.part_id
JOIN items_shipped c
ON a.part_id = c.part_id
WHERE ...
GROUP BY part_num
ORDER BY part_id

Related

How to fetch 5 most recent values and number of count in a column SQL

According to the table, I have to count the top 3 most frequent bookID and the count of the bookID.
I have figured out how to get the top 3 , however I couldn't figured how to display the number of count in the bracket after the comma.
I have to put these into the brackets separate by commas, it look like this:
[top1 bookid , the number of count] => [17,6]
[top2 bookid , the number of count] => [13,6]
[top3 bookid , the number of count] => [16,5]
$query ="SELECT bookID FROM issue_books GROUP BY bookID ORDER BY COUNT(*) DESC LIMIT 3 ";
$query_run = mysqli_query($connection,$query);
foreach ($query_run as $row)
{
echo '['". $row['bookID'];.",".xxxxx."']';
}
Is there any way to display the count of the frequency number together?
You could add the count(*) to the select list:
$query =
"SELECT bookID, COUNT(*) AS cnt FROM issue_books GROUP BY bookID ORDER BY COUNT(*) DESC LIMIT 3 ";
$query_run = mysqli_query($connection,$query);
foreach ($query_run as $row)
{
echo '[' . $row['bookID']; . ',' . $row['cnt'] . ']';
}
Add the count to the select clause?
$query = "SELECT bookID, COUNT(*) AS cnt FROM issue_books GROUP BY bookID ORDER BY cnt DESC LIMIT 3 ";
$query_run = mysqli_query($connection, $query);
foreach ($query_run as $row) {
echo "[" . $row['bookID'] . ", " . $row['cnt'] . "]\n";
}

Getting information from multiple using MySQLI and be able to echo results

for the past few hours, I have been trying to find a simple method of while loop echoing information from multiple tables at once. I'd like to say I've not pulled all my hair out looking, but I have.
Here is one mysqli query to get the following fields from CUSTOMER
$tid = $_SESSION['user_id']; // "id" is 1 for example
$query = "SELECT * FROM `CUSTOMER` WHERE user_id = {$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['user_id'] . "<br><br>";
echo $row['c_fname'] . "<br>";
echo $row['c_sname'] . "<br>";
};
Here is another mysqli query to get the following fields from SALE
$query = "SELECT * FROM `SALE` WHERE user_id = {$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['s_date'] . "<br>";
echo $row['s_total'] . "<br>";
};
Could someone possibly show me how I can get both of these tables in one query so that echoing both tables information is possible at the same time instead of separately. I am not fussed how it is done, As long as it gets all from both tables for echoing purpose, that is good.
You can do it by using LEFT JOIN like this.
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
And this is your code.
$query = "SELECT * FROM `CUSTOMER` LEFT JOIN `SALE` ON `SALE`.user_id=`CUSTOMER`.user_id WHERE `SALE`.user_id={$tid}";
$results = mysqli_query($dbconnection, $query);
while ($row = mysqli_fetch_array($results)) {
echo $row['user_id'] . "<br><br>";
echo $row['c_fname'] . "<br>";
echo $row['c_sname'] . "<br>";
echo $row['s_date'] . "<br>";
echo $row['s_total'] . "<br>";
}
For more info read this,
https://www.w3schools.com/sql/sql_join_left.asp
I hope this helps.
EDITED
This is for joining 3 tables,
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
LEFT JOIN table3 ON table1.column_name = table3.column_name;
In your code.
SELECT * FROM `CUSTOMER`
LEFT JOIN `SALE` ON `CUSTOMER`.user_id = `SALE`.user_id
LEFT JOIN `PRODUCTS` ON `CUSTOMER`.user_id = `PRODUCTS`.user_id
WHERE `SALE`.user_id={$tid};
As variable.
$query = "SELECT * FROM `CUSTOMER` LEFT JOIN `SALE` ON `CUSTOMER`.user_id = `SALE`.user_id LEFT JOIN `PRODUCTS` ON `CUSTOMER`.user_id = `PRODUCTS`.user_id WHERE `SALE`.user_id={$tid}";
You can use the following code and will help u solve Ur problem
$query = "SELECT C.*,S.* FROM CUSTOMER C,SALES S
WHERE C.user_id={$tid}
and C.user_id=S.user_id;
while ($row = mysqli_fetch_array($results)) {
echo $row['C.user_id'] . "<br><br>";
echo $row['C.c_fname'] . "<br>";
echo $row['C.c_sname'] . "<br>";
echo $row['S.s_date'] . "<br>";
echo $row['S.s_total'] . "<br>";
};
You can simply join the tables to get your expected result as shown below.
$query = "SELECT c.user_id, c.c_fname, c.c_sname, s.s_date, s.s_total FROM `CUSTOMER` AS c INNER JOIN `SALE` AS s ON c.user_id = s.user_id WHERE c.user_id = {$tid}";
Joining 3 tables example
$query = "SELECT *
FROM `CUSTOMER` AS c
INNER JOIN `SALE` AS s ON c.user_id = s.user_id
INNER JOIN `PRODUCTS` AS p ON p.product_id = s.product_id
WHERE c.user_id = {$tid}";

Getting data from my join table

I have a join table which takes the id from my respondents table respondant_id and the id from my teams table table_id.
The output is fine when I SELECT from that table so I get back the respondants ID married up with the teams ID.
I am wanting to show the respondents name from respondant_data and the team name from teams by using the values output from the join table.
I have attempted this here but I keep getting 0 results.
$sql = "
SELECT
respondant_data.respondant_id, teams.team_id
FROM
respondant_data
INNER JOIN
teams
ON
respondant_data.respondant_id = teams.team_id
WHERE
respondant_teams.team_id= 5";
$result = $conn->query($sql);
$i = 1;
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $i++ . ' ';
echo 'user_id: ' . $row["respondant_id"] . ', ';
echo 'team_id: ' . $row["team_id"];
echo '<br>';
}
} else{
echo 'no results';
}
So I want my output to be like 'John Smith', 'Central Team'
Try this query.
SELECT
resp_data.respondant_id, teams.team_id
FROM
respondant_data resp_data,
teams,
respondant_teams resp_teams
WHERE
resp_data.respondant_id = teams.team_id
and resp_teams.team_id = teams.team_id
and resp_teams.team_id = 5

PHP - SQL Query filter+

I am working on a php script that will get the required information and display it in an xml. For some reason my brin isn't work and I don't know how to do the query. The database has two tables that I want to pull information from. Both tables have one thing in common which is 'id_member'. Themes table has a bunch of junk in it - so it has 4 coulms - 'id_member', 'id_theme', 'varible', and 'value'. There are two items in 'varible' that I want to filter (show them and not the rest) "cust_armaus" and "cust_armamo" then value will show the value of course. So in the table Themes it will have id_member listed more than once to show the different varibles. This is what I got that isn't working for me:
<?php
header('Content-Type: text/xml');
$username="database_username";
$password="database_password";
$database="database_name";
mysql_connect('localhost',$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
echo "<?xml version=\"1.0\"?>\n".
"<!DOCTYPE squad SYSTEM \"squad.dtd\">\n".
"<?xml-stylesheet href=\"squad.xsl?\" type=\"text/xsl\"?>\n";
?>
<squad nick="Team Tag">
<name>Team Name</name>
<email>contact#team.com</email>
<web>http://www.team.com</web>
<picture>teamlogo.paa</picture>
<title>This is the Team motto</title>
<?php
// smf_themes -> id_member =
// smf_themes -> variable for cust_armaus & cust_usermo
// smf_themes -> value <- get for cust_armaus & cust_usermo
// smf_members -> real_name = profile_name & name
// smf_members -> email_address = email - NO
$memberSQL = mysql_query("SELECT id_member AS id, real_name FROM smf_members");
$member = mysql_fetch_array($memberSQL);
$armausSQL = mysql_query("SELECT id_member, variable, value AS arma_id FROM smf_themes WHERE id_member='$member[id]' AND variable='cust_armaus'");
$armaid = mysql_fetch_array($armausSQL);
$armamoSQL = mysql_query("SELECT id_member, variable, value AS motto FROM smf_themes WHERE id_member='$member[id]' AND variable='cust_usermo'");
$armamo = mysql_fetch_array($armamoSQL);
$num=mysql_numrows($member, $armaid, $armamo);
mysql_close();
$i=0;
while ($i < $num) {
$profile_id = mysql_result($armaid,$i,"value");
$profile_name = mysql_result($member,$i,"real_name");
$profile_remark = mysql_result($armamo,$i,"value");
$profile_username = mysql_result($member,$i,"real_name");
//$profile_email = mysql_result($result,$i,"email");
//$profile_icq = mysql_result($result,$i,"icq");
echo "<member id=\"$profile_id\" nick=\"$profile_name\">" .
"<name>$profile_name</name>".
"<email>$profile_email</email>".
"<icq>$profile_icq</icq>".
"<remark>$profile_remark</remark>".
"</member>\n";
$i++;
}
?>
</squad>
The mysql interface is deprecated. New development should use either mysqli or PDO.
Assuming you only one one value of the cust_armaus and cust_usermo rows for each smf_members, you could use correlated subqueries in the SELECT list, and return just a single query that returns a single row for each row in smf_members. Something like this:
SELECT m.id_member AS id
, m.real_name
, (SELECT a.value
FROM smf_themes a
WHERE a.id_member = m.id_member
AND a.variable = 'cust_armaus'
ORDER BY a.value LIMIT 1
) AS cust_armaus
, (SELECT u.value
FROM smf_themes u
WHERE u.id_member = m.id_member
AND u.variable = 'cust_usermo'
ORDER BY u.value LIMIT 1
) AS cust_usermo
FROM smf_members m
ORDER BY m.id_member
If the (id_member,variable) tuple is guaranteed to be unique in smf_themes, you could use a simpler outer join:
SELECT m.id_member AS id
, m.real_name
, a.value AS cust_armaus
, u.value AS cust_usermo
FROM smf_members m
LEFT
JOIN smf_themes a
ON a.member_id = m.member_id AND a.variable = 'cust_aramus'
LEFT
JOIN smf_themes u
ON u.member_id = m.member_id AND u.variable = 'cust_usermo'
ORDER BY m.id_member
With either of those queries, you could use code something like this:
$sql = "SELECT ...";
$rs = $mysqli->query($sql)
if (!$rs) {
// handle error
echo 'query failed: (' . $mysqli->errno . ') ' . $mysqli->error;
die;
}
// loop through rows returned
while ( $row = $rs->fetch_assoc() ) {
echo '<member id="' . htmlspecialchars($row['id']) . '"'
. ' nick="' . htmlspecialchars($row['real_name']) . '">'
. '<cust_aramus>' . htmlspecialchars($row['cust_aramus']) . '</cust_aramus>'
. '<cust_usermo>' . htmlspecialchars($row['cust_usermo']) . '</cust_usermo>' ;
}

Working with output from joined tables with php oop

As part of my learning OOP PHP, I have made a database object that includes the following method:
public static function find_by_sql($sql="") {
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_array($result_set)) {
$object_array[] = static::instantiate($row);
}
return $object_array;
}
and I can use this to retrieve and access data from a single table, however when I try to use it with joined tables, the object only gives me the data from the primary table e.g.
$sql = "SELECT s.name, m.id, m.firstName, m.lastName, m.dob";
$sql .= " FROM members AS m";
$sql .= " LEFT JOIN mbr_sections AS ms ON m.id = ms.member_id";
$sql .= " LEFT JOIN sections AS s ON ms.section_id = s.id";
$sql .= " ORDER BY s.organisation ASC, s.name ASC, m.lastName ASC, m.firstName ASC";
$sql .= " LIMIT {$per_page} ";
$sql .= " OFFSET {$pagination->offset()}";
$members = Member::find_by_sql($sql);
Using the above query the following code outputs nothing for the s.name field, but all the fields from the members table are correctly listed. I know that the MySQL query is accessing the data, as the ORDER BY statement is correctly sorting the output.
<?php foreach($members as $member): ?>
<tr>
<td><?php echo $member->name;?></td>
<td><?php echo $member->full_name();?></td>
<td><?php echo $member->getAge($member->dob);?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php endforeach; ?>
If I output $members with print_r($members) it only contains the data from the members table, how do I access the data retrieved from the other tables?
Thanks
You need to select them too here:
$sql = "SELECT s.name, m.id, m.firstName, m.lastName, m.dob";
$sql .= " FROM members AS m";
$sql .= " LEFT JOIN mbr_sections AS ms ON m.id = ms.member_id";
$sql .= " LEFT JOIN sections AS s ON ms.section_id = s.id";
$sql .= " ORDER BY s.organisation ASC, s.name ASC, m.lastName ASC, m.firstName ASC";
$sql .= " LIMIT {$per_page} ";
$sql .= " OFFSET {$pagination->offset()}";
$members = Member::find_by_sql($sql);
You only selected name, id, firstName, lastName and dob.
Here is an example:
$sql = "SELECT s.name, m.id, m.firstName, m.lastName, m.dob, mbr_sections.field_you_want, sections.*";

Categories