From Database language to PHP - php

I need some help please. I have this from my database:
SELECT DISTINCT
iwi.item Item, ppd.item_des Des, iwi.qty_on_hand QOH, iwi.qty_allocated QA,
iwi.qty_on_order QOO, iwi.min_qty Minimum, MAX(ppm.expected_date)Exp_Date
FROM inv_warehouse_items iwi, pur_po_master ppm, pur_po_detail ppd)
WHERE IWI.CO = '100'
AND iwi.item_key = ppd.item_key
AND ppm.po_key = ppd.po_key
AND iwi.warehouse = '01'
AND iwi.item IN ('BXHP335',
'BXHP435',
'BXHP535',
'BXHP644',
'BXHP743',
'BXHP965',
'BXHP10741',
'BXHP10748',
'BXHP1253',
'BXHP1257',
'BXHP121210',
'BXHP151410',
'BXHP16114',
'BXHP181411',
'BXHP241612',
'BX1195',
'BX6636',
'BXHP201512',
'BXHP1949',
'BXHP2015',
'BXHP201515',
'BXHP351011',
'BXHP241814',
'BXHP2257',
'BXHP1824')
GROUP BY iwi.item, iwi.qty_on_hand, iwi.qty_allocated, iwi.qty_on_order,
iwi.min_qty, ppd.item_des
ORDER BY iwi.item"
And I need it to print out with php.

May be like this
$query = "...";
$result = $mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($result)) {
print_r($row);
}

Related

Why while loop does not showing result after first iteration inside while loop?

I'm trying to fetch the comments inside the while loop by newsfeed_id, but it does not showing any result after first iteration. I print_r dynamically generated query its working fine but result does not showing.
$NewsfeedRes = array();
$Newsfeed = "select * from `ws_newsfeed` where `nf_status` = 1";
$NewsfeedQuery = mysqli_query($this->connection,$Newsfeed);
while($rowNews = mysqli_fetch_assoc($NewsfeedQuery)){
$NewsfeedRes[] = $rowNews;
$PushComment = "SELECT cmt.`cmt_id`,cmt.`cmt_comment`,cmt.`date_added`,
us.`u_username`,us.`u_image`
FROM `ws_comments` AS cmt
LEFT JOIN `ws_user` AS us ON cmt.`u_id`=us.`u_id`
WHERE cmt.`cmt_target_id` = ".$rowNews['nf_id']."
AND cmt.`cmt_table_name`='ws_newsfeed'";
//echo $PushComment; This giving me correct query
$PushCommentQuery = mysqli_query($this->connection,$PushComment);
while($rowPComment = mysqli_fetch_assoc($PushCommentQuery)){
$NewsfeedRes['comments'] = $rowPComment;
}
}
$output = array(
'NewsfeedRes' => $NewsfeedRes,
);
echo json_encode($output, JSON_PRETTY_PRINT);
Can any one guide me where I'm wrong that i can fix the issue. I will appreciate. Thanks
Every time you loop through your fetch comment loop you are resetting the the value of $NewsfeedRes['comments']. You need to push $rowPComment into an array.
If you want $rowNews and comment to be in the same array try:
while($rowNews = mysqli_fetch_assoc($NewsfeedQuery)){
$PushComment = "SELECT cmt.`cmt_id`,cmt.`cmt_comment`,cmt.`date_added`,us.`u_username`,us.`u_image` FROM `ws_comments` AS cmt LEFT JOIN `ws_user` AS us ON cmt.`u_id`=us.`u_id` WHERE cmt.`cmt_target_id` = ".$rowNews['nf_id']." AND cmt.`cmt_table_name`='ws_newsfeed'";
//echo $PushComment; This giving me correct query
$PushCommentQuery = mysqli_query($this->connection,$PushComment);
while($rowPComment = mysqli_fetch_assoc($PushCommentQuery)){
$rowNews['comments'][] = $rowPComment;
}
$NewsfeedRes[] = $rowNews;
}
If you want a seperate array for comments and news feed try:
while($rowNews = mysqli_fetch_assoc($NewsfeedQuery)){
$NewsfeedRes["newsfeed"][] = $rowNews;
$PushComment = "SELECT cmt.`cmt_id`,cmt.`cmt_comment`,cmt.`date_added`,us.`u_username`,us.`u_image` FROM `ws_comments` AS cmt LEFT JOIN `ws_user` AS us ON cmt.`u_id`=us.`u_id` WHERE cmt.`cmt_target_id` = ".$rowNews['nf_id']." AND cmt.`cmt_table_name`='ws_newsfeed'";
//echo $PushComment; This giving me correct query
$PushCommentQuery = mysqli_query($this->connection,$PushComment);
while($rowPComment = mysqli_fetch_assoc($PushCommentQuery)){
$NewsfeedRes['comments'][] = $rowPComment;
}
}

Mysqli query doesn't work with id from another table

I have this php script.
$cwZ = count($wiegen_zutat);
$cwM = count($wiegen_menge);
$cwS = count($wiegen_schritt);
if($cwM == $cwS and $cwM == $cwZ and $cwZ == $cwS){
for($x = 0; $x < $cwZ; $x++){
$aktZuat = $wiegenZutat[$x];
$qr = "SELECT ID_Zutat FROM Zutaten WHERE Name='$aktZutat' LIMIT 1";
$id_get = mysqli_query($verbindung,$qr );
$id = mysqli_fetch_array($id_get);
$zuatenID = $id['ID_Zutat'];
echo $id['ID_Zutat'];
echo $zutatenID;
$sql3 = "INSERT INTO Wiegen (ID_Zutat, Menge) VALUES ('$zutatenID', '$wiegenMenge[$x]')";
$wiegenEintragen = mysqli_query($verbindung, $sql3);
}
}
$wiegen_zutat, _menge, _schritt are all three arrays which contain the information from my form.
I go through the first array, and check the variable against a table which contains the ingredients for my website. I want to get the id of a ingredient which was added some steps before and add it into another table.
The problem is that neither the echos or the query are working.
What am I missing?
Please don't get confused by the name of the variables, I'm german :)
Best regards

MySQL sorting with PHP

I'm trying to accomplish the following situation:
$mysql_query = "
SELECT *
FROM st_users
WHERE
`user_comp_supervisor_id` = '$team_supervisor' AND
`user_exempt_from_goals` = '0'
ORDER BY 'calculate_progress_percent()' ASC
";
I know that I can't accomplish ordering by a function in a MySQL statement, but I'm trying to figure out how to take all the returned records, and then order them in order of highest to lowest from a php function result. Any ideas would be greatly appreciated; I've been trying to wrap my head around this for a few hours now... :-(
function diy_calc_progress_percent($user_id,$period_id,$period_week_number)
{
$this->user_id = $user_id;
$this->period_id = $period_id;
$this->period_week_number = $period_week_number;
if ($this->period_week_number == 1)
{
$this->week_id = mysql_result( mysql_query(" SELECT `period_week_one` FROM `st_comp_periods` WHERE `period_id` = '$this->period_id' "),0 );
}
else if ($this->period_week_number == 2)
{
$this->week_id = mysql_result( mysql_query(" SELECT `period_week_two` FROM `st_comp_periods` WHERE `period_id` = '$this->period_id' "),0 );
}
else
{
echo "Week number not valid.";
exit();
}
$this->week_start_date = mysql_result( mysql_query(" SELECT `week_start_date` FROM `st_comp_weeks` WHERE `week_id` = '$this->week_id' "),0 );
$this->week_end_date = mysql_result( mysql_query(" SELECT `week_end_date` FROM `st_comp_weeks` WHERE `week_id` = '$this->week_id' "),0 );
$this->user_department = $this->user_info($this->user_id,"user_comp_department_id");
$this->user_week_diy_goal = mysql_result( mysql_query(" SELECT `goal_diy_department` FROM `st_comp_department_goals` WHERE `goal_department_id` = '$this->user_department' AND `goal_week_id` = '$this->week_id' "),0 );
$this->calc_totals_result = mysql_query("SELECT SUM(record_total_diy_revenue) AS user_week_total FROM `st_entered_records` WHERE `record_user_id` = '$this->user_id' AND `record_date` BETWEEN '$this->week_start_date' AND '$this->week_end_date'");
$this->calc_totals_row = mysql_fetch_assoc($this->calc_totals_result);
$this->user_week_total = $this->calc_totals_row['user_week_total'];
$this->user_week_one_percent = ($this->user_week_total / $this->user_week_diy_goal) * 100;
$this->user_week_one_percent = number_format( (float)$this->user_week_one_percent, 2, '.', '' );
return $this->user_week_one_percent;
}
You probably will have to do some array juggling.
First get all your entries FROM st_users into a first array (mysql_query)
Then you could run through that array, and for each entry you do the calculate_progress_percent() and build up a second array in which you could add the additional info ("user_progress_percent").
After this you can sort the new array ba your new info ("user_progress_percent").
And here is some quick and dirty code-suggestions – code is however not tested… of course…:)
First:
$mysql_query = "SELECT * FROM st_users
WHERE `user_comp_supervisor_id`='$team_supervisor' AND
`user_exempt_from_goals` = '0'";
Then something like this:
$i = 0;
while($tmp = mysql_fetch_array($mysql_query)) {
$my_second_array[$i]['user_id'] = $tmp['user_id'];
$user_id = $my_second_array[$i]['user_id'];
diy_calc_progress_percent($user_id,$period_id,$period_week_number);
$my_second_array[$i]['user_result'] = $diy_calc_progress_percent_result;
$i++;
}
And then sorting that second array should be possible as described here:
Sort Multi-dimensional Array by Value
…hope this helps at some point…

SQL Query not ordering results properly

I'm querying my database to return a set of results from highest to lowest. But, it seems as if the query isn't entirely ordering the results that way. My code is as follows:
$query = mysql_query("SELECT * FROM ".$stats_table." ORDER BY ppg DESC")or die(mysql_error());
$count = mysql_num_rows($query);
$i = 0;
while($row = mysql_fetch_assoc($query))
{
$team[$i] = $row['team'];
$ppg[$i] = $row['ppg'];
$i++;
}
for($i=0;$i<$count;$i++)
{
echo "".$ppg[$i]." <br /><br />";
}
When I echo out the ppg column set I get the following:
99.7
98.2
97.8
97.4
97.1
96.9
96.8
96.3
96.2
95.5
94.6
94.5
94.3
93.9
93.2
92.8
92.2
91.5
90.8
90.3
106.0
105.9
104.5
103.6
102.6
101.9
101.5
101.3
100.7
100.1
It seems that for all of the values less than 100 and over 100 it works. But, how can I make this order work with all values instead of just within those less than and above 100?
Thanks,
Lance
Change the data type to numeric like double, real, float. Try it.
Make ppg double, real or anything numeric, not varchar.
Use ORDER BY ABS(ppg) if you don't want to touch your structure
You could let PHP sort it.
$query = mysql_query("SELECT * FROM ".$stats_table)or die(mysql_error());
$i = 0;
while($row = mysql_fetch_assoc($query)) {
$team[$i] = $row['team'];
$ppg[$i] = (int)$row['ppg'];
$i++;
}
sort($ppg, SORT_NUMERIC);
for($i=0;$i<count($ppg);$i++) {
echo $ppg[$i]." <br /><br />";
}

SELECT COUNT(a.id) AS id... => Result array is empty! [PHP, MySQL]

I'm having trouble with some PHP since yesterday, looked through the web and had the stupid feeling that I'm missing something important.
Using mysql_fetch_object usually, tried it with mysql_fetch_array though (did not help). Here's the part of the code which gives me an headache:
public static function get_datacenter_by_id($id) {
$result = mysql_query("SELECT COUNT(rack.id) AS Racks, COUNT(device.id) AS Devices, COUNT(card.id) AS Cards, COUNT(port.id) AS Ports
FROM datacenter, rack, device, card, port, location, building
WHERE location.id = building.location_id AND
building.id = datacenter.building_id AND
datacenter.id = '.$id.' AND
rack.id = device.rack_id AND
device.id = card.device_id AND
(card.id = port.card_id1 OR
card.id = port.card_id2)") or die ("Error in query: ".mysql_error());
$array = array();
while($row = mysql_fetch_object($result)) {
$array[] = array($row->Racks, $row->Devices, $row->Cards, $row->Ports);
}
return $array;
}
$array is used in another .php file, but using print_r $array already shows you, that the array stays empty (0). I'm quite sure that the error appears in this block of code, could "COUNT (x) AS y" be at fault?
PS: The MySQL Query works, tested it via Workbench before. I'd appreciate some good adivce! :-)
Have a nice day!
Isn't this as simple as:
$result = mysql_query("SELECT COUNT(rack.id) AS Racks, COUNT(device.id) AS Devices, COUNT(card.id) AS Cards, COUNT(port.id) AS Ports
FROM datacenter, rack, device, card, port, location, building
WHERE location.id = building.location_id AND
building.id = datacenter.building_id AND
datacenter.id = '" . $id . "' AND
rack.id = device.rack_id AND
device.id = card.device_id AND
(card.id = port.card_id1 OR
card.id = port.card_id2)") or die ("Error in query: ".mysql_error());
Note the modification from '.$id.' to '" . $id . "'. Your query is looking for a data centre ID of '.$id.'.
Instead of using it like this
while($row = mysql_fetch_object($result)) {
$array[] = array($row->Racks, $row->Devices, $row->Cards, $row->Ports);
}
You should do this
$row = mysql_fetch_object($result)
$array[] = $row->Racks;
$array[] = $row->Devices;
$array[] = $row->Cards;
Because you are fetching 1 record and using it in while is causing problem

Categories