I am trying to figure out why my query is not working. I am trying to add all the amounts together for each month where status = 'S'. However, I get the following error. Any ideas?
[05-Jul-2013 11:21:30 America/New_York] PHP Fatal error: Cannot use object of type mysqli_result as array
My Code:
$closedsales = mysqli_query($mysqli, "SELECT MONTH(date) as month, sum(amount) as total FROM sales WHERE user_id = '".$userid."' AND status = 'S' GROUP BY MONTH(date)");
while ( $row = mysqli_fetch_row($closedsales) ) {
$closedsales[$row['month']] = $row['total'];
}
UPDATE:
$closedsales = mysqli_query($mysqli, "SELECT MONTH(date) as month, sum(amount) as total FROM sales WHERE user_id = '".$userid."' AND status = 'S' GROUP BY MONTH(date)");
while ( $row = mysqli_fetch_row($closedsales) ) {
$monthlysales[$row['month']] = $row['total'];
}
foreach($monthlysales as $monthlysale) {
echo $monthlysale;
echo "This worked...";
}
Thanks! I got rid of that error. However, for some reason my array does not contain anything. It only prints out This worked...
Is it possible this is because I don't have each month in the DB?
The problem is the assignment:
$closedsales[$row['month']] = $row['total'];
$closedsales is the result returned by mysqli_query, it's not an array you can assign to. Use a different variable for this.
Related
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
I am working on one of my old php projects where I used GROUP BY head_id that was working fine, but now it's showing nothing but blank page while executing. Showing results while I am trying GROUP BY id instead of head_id or anything else.
if(isset($_POST['submit'])) {
$start = mysqli_real_escape_string($conn, $_POST['start']);
$end = mysqli_real_escape_string($conn, $_POST['end']);
$sql = "SELECT *
FROM bill_info
WHERE date(updated_on) BETWEEN '$start' AND '$end' AND is_paid='1'
GROUP BY head_id";
$res = mysqli_query($conn, $sql);
}
while ($data = mysqli_fetch_array($res)) {
echo $data['head_id']." - ".$data['amount'];
}
Only if I am trying to GROUP BY id then it's working fine but showing nothing while trying to GROUP BY head_id. There are lots of rows for same head_id, that's why I need to group them, otherwise report is becoming too long.
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||-----------------------------------------------------------
Updated !!! ---------------------------------------------------------
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The query given below is working fine for me.
$sql = "SELECT head_id, sum(amount), sum(student_no)
FROM bill_info
WHERE date(updated_on) BETWEEN '$start' AND '$end' AND is_paid='1'
GROUP BY head_id";
I have tried to select other columns except head_id or aggregate function, but it doesn't work!
Try This
if(isset($_POST['submit'])) {
$start = mysqli_real_escape_string($conn, $_POST['start']);
$end = mysqli_real_escape_string($conn, $_POST['end']);
$sql = "SELECT id, head_id, is_paid, amount, sum(student_no), sum(amount) AS sum
FROM bill_info
WHERE updated_on>='$start'
AND updated_on<='$end'
GROUP BY id";
$res = mysqli_query($conn, $sql);
}
while ($data = mysqli_fetch_array($res)) {
$sum = $row['sum'];
}
echo $sum."<br>";
I have some problem with MySQL syntax in CI this is my table
id_data | nama_depan | nama_belakang | gender | luas_lantai | jenis_lantai
| status
I already tried code bellow and show error
Message: Object of class CI_DB_mysqli_result could not be converted to
int
public function getStatus1($status){
$str = "SELECT count(status)
FROM data_latih WHERE status = 1 ";
$str1 = "SELECT count(status)
FROM data_latih ";
$query = $this->db->query($str);
$query1 = $this->db->query($str1);
return $query / $query1;
}
expected output is decimal number of count status = 1 divided by count all rows
EDIT : Found the answer, thank you so much guys i'm new in programming so bear with me :)))
The problem is that you are trying to divide an Object by the Object which does not make any sense, Try this CI way,
public function getStatus1($status){
$this->db->count_all_results('data_latih ');
$this->db->where('status ', '1');
$this->db->from('data_latih ');
$result1 = (float)$this->db->count_all_results();
$result2 = (float)$this->db->count_all('data_latih');
return $result1 / $result2 ;
}
You can actually handle this with a single query:
SELECT
SUM(status = 1) / COUNT(status) AS result
FROM data_latih;
Regarding your PHP code, I would recommend assigning aliases to the various counts, and then accessing those aliases.
$sql = "SELECT SUM(status = 1) / COUNT(status) AS result FROM data_latih";
$result = $this->db->query($sql);
if ($row = $result->fetch_array()) {
echo $row['result'];
}
From the code below, this what I'm trying to achieve: If citizenID found by the $get_friends query are 1 and 2. I would like the while loop to first get all the rows for 1 and then do the same for 2. The code I currently have below its only retrieving the last row for 1 and 2. Please help.
<?php
include ('session_start.php');
include_once('db_connect_universal.php');
$user_id = $_SESSION['sess_id'];
//GET USER FRIENDS AND DETAILS
$get_friends = "SELECT * FROM citizens
INNER JOIN user_details
ON citizens.citizenID=user_details.UserID
WHERE citizens.userID = '".$user_id."'";
$results_get_friends = $conn->query($get_friends);
$num_rows_friends = mysqli_num_rows ($results_get_friends);
while ($row_friends = $results_get_friends->fetch_assoc()) {
$citizenID = $row_friends['citizenID'];
//GET RATINGS AND COMMENTS
$sql = "SELECT * FROM comments
INNER JOIN organiser ON comments.movieID=organiser.movieID
WHERE comments.userID= '".$citizenID."'
AND ratings_n_comments.Time BETWEEN DATE_SUB(CURDATE(), INTERVAL 28 DAY)
AND DATE_ADD(CURDATE(), INTERVAL 10 DAY)
ORDER BY comments.Time DESC";
$result = $conn->query($sql);
$num_rows = mysqli_num_rows ($result);
$row = $result->fetch_assoc();
$n_Rating[]= array(
'dp' => $row_friends['display_pics'],
'uname' => $row_friends['Uname'],
'poster'=> $row['mooster'],
'title'=> $row['moitle'],
'genre'=> $row['moenre'],
'comment'=> $row['Comment'],
'mid'=> $row['mID'],
'check' => 'data'
);
}
header('Content-Type: application/json');
echo json_encode ($n_Rating);
}
//detailed error reporting
if (!$sql) {
echo 'MySQL Error: ' . mysqli_error($conn);
exit;
}
?>
You are only fetching one row with $row = $result->fetch_assoc();.
Are you trying to fetch all the comments for the users?
Because then you'll need to loop trough everything $result has just like you are doing with while ($row_friends = $results_get_friends->fetch_assoc())
Also instead of concatenating your SQL query, try using prepare and bind_param to avoid possible SQL injection
I’m using this code:
$date = date('Y-m-d');
$selStat = "SELECT obqva_id, COUNT(*) as broqch FROM statist WHERE date='$date' GROUP BY obqva_id ORDER BY COUNT(*) DESC LIMIT 8 ";
$queryStat = mysqli_query($conn, $selStat);
while ($rowStat = mysqli_fetch_array($queryStat)) {
$count = $rowStat['broqch'];
$id = $rowStat['obqva_id'];
echo $id.' - '.$count.'<br>';
$selBest = "SELECT * FROM view WHERE id='$id' GROUP BY $count ";
$queryBest = mysqli_query($conn, $selBest);
**$rowView = mysqli_fetch_array($queryBest);** this problem !
$selImage = "SELECT * FROM upload WHERE obqva_id='$id'";
$queryImage = mysqli_query($conn, $selImage);
$rowImage = mysqli_fetch_array($queryImage);
?>
Which produces this output:
First and next result has a problem, three and next have no problem... why?
First number 41 is ID next number total view.
it seems that the query is not valid, or there was no result.
thats why you get a bool instead of a resource.
you can filter that by putting your mysqli_query function in an IF statement, like this:
$selBest = "SELECT * FROM view WHERE id='$id' GROUP BY $count ";
if($queryBest = mysqli_query($conn, $selBest)){
$rowView = mysqli_fetch_array($queryBest);
}
else{
$rowView = false;
}
That's that, now for the query. You are trying to group the result on a number:
SELECT * FROM view WHERE id='$id' GROUP BY $count
Not sure why you want to group, as it seems that you only want to get some info on a specific id. In that case, i would get rid of the GROUP BY statement.
I have column qty in my table, and I want to sum up all the qty from this column in a variable:
$get_sold = "select qty from pending_orders where product_id='$p_id' AND order_status=='done'";
$result = mysqli_query($con, $get_sold);
try this.
$get_sold = "select sum(`qty`) as sum from pending_orders where product_id='$p_id' AND order_status='done'";
$result = mysqli_query($con,$get_sold);
$row=mysqli_fetch_assoc($result);
echo $row['sum'];
First you need sum to your column in your query by using sum(qty)as totals and also remove == from order_status it is =
you need to use mysqli_fetch_row to get your sum data into your variable
<?php
$get_sold = "select sum(`qty`)as totals from `pending_orders` where `product_id`='".$p_id."' AND `order_status`='done'";
$result = mysqli_query($con,$get_sold);
$row=mysqli_fetch_row($result);
$row[0];// get your total sum
try using, MySql sum function
$get_sold = "select sum(qty) total_qty from pending_orders where product_id='$p_id' AND order_status='done'";
$result = mysqli_query($con,$get_sold);
you should use
select sum(qty) as sumqty from pending_orders where product_id='$p_id' AND order_status='done'
Also mention, that in mysql you need to write on equal sign instead of two
Most people have responded here with a quick copy/paste, but I feel like it would be better suited to format your query nicely for readability sake to help you identify the issue.
$get_sold = "
SELECT SUM(`qty`) as `total_quantity`
FROM `pending_orders`
WHERE `product_id` = '$p_id' AND `order_status` = 'done'
";
$result = $con->query($get_sold);
if ( !$result ) {
die("MySQL error: " . $con->error);
}
$row = $result->fetch_assoc();
$quantity = $row['total_quantity'];
CHANE QUERY , SUM(column_name) gives sum of all numbers in column_name
$get_sold = "select SUM(qty) as total from pending_orders where product_id='$p_id' AND order_status='done'";