I have a defined array. If is one from column (e.g. Bayern 'by') empty, then i do not receive variable $row_wohn_by and after in result get is: Notice: Undefined variable: row_wohn_by
Simply erase is not a solution for another operate. Expected is defined $row_wohn_by with 0 (zero) number.
Here is code:
$states = array(
'Baden-Württemberg' => 'bw',
'Bayern' => 'by',
'Berlin' => 'be',
'Thüringen' => 'th'
);
$numb_rows = mysqli_query($conn, 'SELECT COUNT(*)
FROM members
WHERE priv_staat = "Deutschland"
');
$numb_row = mysqli_fetch_array($numb_rows);
$total_wohn = $numb_row[0];
$query_wohn = mysqli_query($conn, "SELECT `priv_land`, COUNT(*) AS `count` FROM `members` WHERE `priv_land` !=0 GROUP BY `priv_land`");
while ($item_wohn = $query_wohn->fetch_assoc()) {
${'row_wohn_'.$states[$item_wohn['priv_land']]} = $item_wohn['count'];
${'row_wohn_per_'.$states[$item_wohn['priv_land']]} = number_format((($item_wohn['count'] / $total_wohn)*100), 2, ',', ' '); // calulate in %
}
$row_wohn_all = $total_wohn-($row_wohn_bw + $row_wohn_by + $row_wohn_be + $row_wohn_th);
$row_wohn_per_all = number_format((($row_wohn_all / $total_wohn)*100), 2, ',', ' '); // calulate in %
Thanks for help.
You wrote:
Expected is defined $row_wohn_by with 0
Looking at your query (SELECT priv_land, COUNT(*) AS count FROM members), this can only be the case when there are no rows with priv_land = 'by'. So within your while-loop just after your query, that variable never got set.
Solution: initialize all variables with value 0 and overwrite those with the results from your query:
foreach($states as $privLand) {
${'row_wohn_' . $privLand} = 0;
${'row_wohn_per_' . $privLand} = 0;
}
$query_wohn = mysqli_query($conn, "SELECT `priv_land`, COUNT(*) AS `count` FROM `members` WHERE `priv_land` !=0 GROUP BY `priv_land`");
while ($item_wohn = $query_wohn->fetch_assoc()) {
${'row_wohn_'.$states[$item_wohn['priv_land']]} = $item_wohn['count'];
${'row_wohn_per_'.$states[$item_wohn['priv_land']]} = number_format((($item_wohn['count'] / $total_wohn)*100), 2, ',', ' '); // calulate in %
}
For bonus points, rewriting everything to an array-notaition makes life a lot easier (example for only $row_wohn_*):
// Initialize all keys with value 0
$row_wohn = array_fill_keys($states, 0);
$query_wohn = mysqli_query($conn, "SELECT `priv_land`, COUNT(*) AS `count` FROM `members` WHERE `priv_land` !=0 GROUP BY `priv_land`");
while ($item_wohn = $query_wohn->fetch_assoc()) {
$row_wohn[ $states[$item_wohn['priv_land']] ] = $item_wohn['count'];
}
$row_wohn_all = $total_wohn - array_sum( $row_whon );
Related
I am doing sum with the below query but it is not giving result properly.
If there are four items and it is showing the result like:
1.000 2.000 3.000 4.000 and it should be like 10.000
I don't know where I am mistaken please help.
<?php
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTC = $torder['item_id'];
$qtyT = $torder['qty'];
$chTP = mysql_query("
select * from temp_choices
where item_id = '".$prITTC."'
AND ses_mem = 113
AND status = 1
");
while($chGET = mysql_fetch_array($chTP)){
$fID = $chGET['id'];
$field = $chGET['choice_id'];
$order_tempCHP = mysql_query("
select sum(price) as total, id, ename, choice_id, item_id, price
from choice_price
WHERE
id IN('$field')
");
while ($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
}
?>
according to my question above after trying and trying i found the solution and resolved my problem according to below code:
Thanks to #John Kugelman at MySQL query using an array
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTD = $torder['id'];
$prITTC = $torder['item_id'];
$chTPaa = mysql_query("
select choice_id
FROM temp_choices
WHERE item_id = '$prITTC'
AND ses_mem = 113
AND status = 1
group by choice_id
");
while ($chGETaa = mysql_fetch_assoc($chTPaa)){
$temp[] = $chGETaa['choice_id'];
}
$thelist = implode(",",$temp);
$order_tempCHP = mysql_query("
select sum(price) as total
from choice_price
WHERE
id IN ($thelist)
AND
item_id = '".$prITTC."'
");
while($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
I've been looking for an answer to this all afternoon so far and cannot come up with anything yet.
I have the following MySQL PDO query:
$q = "select recip_id,
sum(case when msg_read = '0' AND msg_deleted = '0' then 1 else 0 end) uu,
sum(case when msg_read = '0' AND msg_deleted = '1' then 1 else 0 end) ud,
sum(case when msg_read = '1' AND msg_deleted = '0' then 1 else 0 end) ru,
sum(case when msg_read = '1' AND msg_deleted = '1' then 1 else 0 end) rd,
count(*) as total
from messages where recip_id = :d GROUP BY recip_id WITH ROLLUP";`
which, when I use recip_id = 18 for an example in PHPMyAdmin gives me the following table:
I have tried several ways to fetch the resulting row in php so that I can use the values for another task, to no avail. I've tried this:
$stmt = $dbo->prepare($q);
$row = $stmt->execute(array(":id" => $id));
$total = $row['total'];
$uu = $row['uu'];
$ud = $row['ud'];
$ru = $row['ru'];
$rd = $row['rd'];
echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";
And this:
$stmt = $dbo->prepare($q);
$stmt->bindParam(":id", $id);
$msgcount = array();
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$total = $row->total;
$uu = $row->uu;
$ud = $row->ud;
$ru = $row->ru;
$rd = $row->rd;
}
echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";
and this too:
$msgcount = array();
$stmt = $dbo->prepare($q);
$stmt->bindParam(":id", $id);
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
array_push($msgcount, array($row['uu'], $row['uu'], $row['ud'], $row['ru'], $row['rd']));
}
echo $msgcount[];
I cannot retrieve the values using my PHP script from the MySQL result set. I've tried serialize() on the rows and whole result set, I've tried to use PDO::FETCH_ASSOC and also unspecified fetch() and fetchAll(). *'ve used different combos and just get an empty result set or *I can't find an answer anywhere either. Can anyone help me with this please?
In your first attempt you forget to $stmt->fetch, and in the last two you forget $stmt->execute. Try this:
$stmt = $dbo->prepare($q);
if ( ! $stmt->execute(array(":id" => $id)) ) die("error"); // returns true or false
$row = $stmt->fetch(); // add this line!
$total = $row['total'];
$uu = $row['uu'];
$ud = $row['ud'];
$ru = $row['ru'];
$rd = $row['rd'];
echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";
I am trying to show a count but only where the column deleted is equal to 0
here is what i have tried
$result=mysql_query("SELECT * FROM users where deleted=0")or die('Error ' );
$counter = mysql_query("SELECT COUNT(*) as personID FROM users");
$row = mysql_fetch_array($result);
$personID = $row['personID'];
$personFname = $row['personFname'];
$personSname = $row['personSname'] ;
$llmail = $row['llmail'];
$mainadmin = $row['mainadmin'];
$delete = $row['delete'];
$num = mysql_fetch_array($counter);
$count1 = $num["personID"];
This shows a count of 4 however of the 4, 2 are deleted so it should only show 2 if this makes sense?
SELECT COUNT(*) as personID FROM users WHERE deleted=0
I'm using SELECT(MAX()) inside a foreach loop and this is my code:
foreach($_POST['image_Basename'] as $key=>$image_Basename){
$image_Title = $this->input->post('image_Title');
$image_Category_Id = $this->input->post('image_Category_Id');
$this->db->query("INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
SELECT 1 + coalesce((SELECT max(image_Group_Id) FROM mg_gallery), 0), '$image_Title', '$image_Basename', '$image_Category_Id'
");
}
The problem is that for each image_Basename, query produces a new number.
For example, if I got 3 image_Basenames, it will insert 1, 2 and 3 for those three image_Basenames. But I want it to insert the same number to all of image_Basenames.
For example, if the max number in the image_Group_Id is 1, then add number 2 for each image_Basename. How can I do that?! I've put
SELECT 1 + coalesce((SELECT max(image_Group_Id) FROM mg_gallery
outside of the foreach loop, but it didn't work!!!
The answer is added below by myself
EDITED 2
Try this if it works or not,
$maxRs = $this->db->query('SELECT max(image_Group_Id) AS max FROM mg_gallery');
echo $this->db->last_query();die; #run this query in your phpmyadmin and debug it.
if($maxRs->num_rows() > 0){
$maxData = $maxRs->row_array();
echo "here :".$maxID = $maxData['max'];die;
}else{
$maxID = 0;
}
//echo "max : ".$maxID;die; #check if its returning the corrent maxid or not.
foreach($_POST['image_Basename'] as $key=>$image_Basename){
$image_Title = $this->input->post('image_Title');
$image_Category_Id = $this->input->post('image_Category_Id');
$this->db->query("INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
$maxID, '$image_Title', '$image_Basename', '$image_Category_Id'
");
echo $this->db->last_query();die; #check the query its generating is correct or not and run directly at phpmyadmin
}
I'm not fully sure what kind of data you have and exactly what you want, but I'll help you towards the right direction:
$int_basename = (int)max($this->input->post('image_Basename'));
$str_image_title = $this->input->post('image_Title');
$str_image_category_id = $this->input->post('image_Category_Id');
$query = $this->db->query("SELECT max(image_Group_Id) AS max FROM mg_gallery");
$int_max = (int)$query->row()->max;
$arr_union = array();
for($i = 1; $i <= 3; $i++)
if ($i == 1)
$arr_union = "SELECT " . ($int_max + $i) . " AS id";
else $arr_union = "SELECT " . ($int_max + $i);
$str_union = implode(' UNION ', $arr_union);
$this->db->query("
INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
SELECT h.id, ?, ?, ?
FROM ({$str_union}) AS h
", array($str_image_title, $int_basename, $str_image_category_id));
This would only run the query twice and spare your database the trouble of loop queries. I also escaped the values through $this->db->query() as intended to avoid mysql injections. This doesn't really require INSERT ... SELECT as INSERT ... VALUES is enough.
First of all, you should never be inserting values directly from the POST array. But in the interest of just addressing the question at hand, I'll leave the code as is.
You need to query for the MAX(image_Group_Id) before starting the loop and not do a + 1 inside the loop. Like this:
$get_group_id = $this->db->query("SELECT 1 + coalesce((SELECT max(image_Group_Id) AS group_id FROM mg_gallery), 0)");
$get_group_id_array = $get_group_id->fetch_assoc();
$group_id = $get_group_id_array['group_id'];
foreach($_POST['image_Basename'] as $key=>$image_Basename){
$image_Title = $this->input->post('image_Title');
$image_Category_Id = $this->input->post('image_Category_Id');
$this->db->query("INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
$group_id, '$image_Title', '$image_Basename', '$image_Category_Id'
");
}
IT WORKS:
Specially thanks to Niloy Saha, finally, I got the answer, and this is the code I've used:
$getMaxValue = $this->db->query('SELECT 1 + coalesce((SELECT MAX(image_Group_Id)), 0) AS image_Group_Id FROM mg_gallery');
if($getMaxValue->num_rows() > 0){
$group_Id = $getMaxValue->row_array();
$image_Group_Id = $group_Id['image_Group_Id'];
}else{
$image_Group_Id = 0;
}
foreach($_POST['image_Basename'] as $key=>$image_Basename){
$image_Title = $this->input->post('image_Title');
$image_Category_Id = $this->input->post('image_Category_Id');
$this->db->query("INSERT INTO mg_gallery (image_title, image_Basename, image_Category_Id, image_Group_Id)
VALUES ('$image_Title', '$image_Basename', '$image_Category_Id', $image_Group_Id)
");
}
Hoping I am just missing something simple here:
$sql = "Select * FROM user_info, user_login WHERE user_login.status = '0' OR user_login.status = '2' AND user_info.uid = user_login.uid";
$db = new connection();
$results = $db->query($sql);
$user = array();
while($info = mysql_fetch_array($results))
{
$user[] = $info;
}
$total = count($user);
//TEST the amount of rows returned.
echo $total;
for ($i = 0; $i < $total; $i++)
{
//echo data;
}
just trying to pull all data that has the user_login.status field set to "0" or "2" but it shows everything thing and it shows the items marked as 2 twice.
Does anyone see my issue?
Your precedence is getting whacked because of missing parentheses:
SELECT DISTINCT *
FROM user_info, user_login
WHERE (user_login.status = '0' OR user_login.status = '2')
AND user_info.uid = user_login.uid
Without seeing the data I can't give you more than a SELECT DISTINCT with regards to the duplicate records.
Select * FROM user_info, user_login WHERE (user_login.status = '0' OR user_login.status = '2') AND user_info.uid = user_login.uid
Order of precedence :)