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>";
Related
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 3 years ago.
The first query works and displays the proper results. When I add an additional AND condition I get a "Trying to get property of a non-object" error.
I can successfully execute with one OR and one other condition.
Here is the query echoed:
SELECT * FROM scores
WHERE (sortDate <= 20190629 AND sport = football)
AND (homeID = 4 OR awayID = 4)
ORDER BY gameDate DESC
I'm sure the variable are passing through.
This one works fine:
$sql = "SELECT * FROM scores
WHERE sortDate <= $today
AND (homeID = $teamID OR awayID = $teamID)
ORDER BY gameDate DESC";
This one gives an error (added AND):
$sql = "SELECT * FROM scores
WHERE (sortDate <= $today AND sport = $sportID)
AND (homeID = $teamID OR awayID = $teamID)
ORDER BY gameDate DESC";
Try this
$sql = "SELECT * FROM scores
WHERE (sortDate <= $today AND sport = '$sportID')
AND (homeID = $teamID OR awayID = $teamID)
ORDER BY gameDate DESC";
OR
$sql = "SELECT * FROM scores
WHERE (sortDate <= $today) AND (sport = $sportID)
AND (homeID = $teamID OR awayID = $teamID)
ORDER BY gameDate DESC";
i am working on a timeline for my website but i am having some problem when i ran the query to select all id that is less than given identifier its still return the identifier result upon every query
example if identifier is id=4 i want to select everything less than 4 and not from 4 > 3 > 2 > 1 i want it to be 3 > 2 > 1
here is my php. i know its not secure or what not but i have written it in prepared statement and get the same thign so i need some here.
if(isSet($_POST['lastmsg']))
{
$feed_id = mysqli_real_escape_string($con, $_POST['lastmsg']);
$get1 = mysqli_query($con, 'SELECT receiver FROM connection where sender="'.$_SESSION['userid'].'"');
$id_feed = array();
while($id_result1 = mysqli_fetch_array($get1)){
$id_feed[] = $id_result1['receiver'];
$ids1 = join(',', $id_feed);
$get_feed1 = mysqli_query ($con, "select * from feed where users in '".$ids1."' or users='".$_SESSION['userid']."' and 'feed_id' < '".$feed_id."' ORDER BY feed_id DESC LIMIT 2");
}
while($res1 = mysqli_fetch_array($get_feed1)){
echo $load = $res1['feed_id'];
}
}
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 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.
I have 2 tables as follow :
table1 has these fields:
year,day,month,name
table2 has these fields:
years,gift
I want to return the rows where year = years. the two queries NEED to be run separately that is a condition.
I CANNOT join them into 1 query (I know how to do that but that's not how this should be done). Any suggestions? I thought of using a foreach loop but failed to implement, any ideas?
$sql1 = " SELECT * FROM table1 ORDER BY id ASC";
$result1 = mysql_query($sql1);
$rows = mysql_fetch_array($result1);
while($rows = mysql_fetch_array($result1))
{
$year[] = $rows['year'];
}
$sqlx = "SELECT * FROM table2";
$result = mysql_query($sqlx);
while($row = mysql_fetch_array($result))
{
$years[] = $rows['years'];
}
This is a strange requirement, since it should be done with a join. It is vastly less efficient to do this with multiple queries in a loop than a single query with a JOIN.
In any case if you must do it this way, you have retrieved the years into an array correctly from your first query -- use that array to populate the query for each loop iteration of your second query:
$year = array();
$sql1 = " SELECT * FROM table1";
$result1 = mysql_query($sql1);
if ($result1) {
$rows = mysql_fetch_array($result1);
while($rows = mysql_fetch_array($result1))
{
$year[] = $rows['year'];
}
}
// Will hold your output
$gifts = array();
// $year is now an array of years.
// Loop over it to query table2:
foreach ($year as $y) {
// Make sure it is an integer...
$y = intval($y);
// And query table2 using $y as a WHERE condition
$sql2 = "SELECT years, gift FROM table2 WHERE years = $y";
$result2 = mysql_query($sql2);
if ($result2) {
while($row = mysql_fetch_array($result2)) {
// Append all rows to the $gifts array
$gifts[] = $row;
}
}
}
// They're now all in the array $gifts.
var_dump($gifts);
Update:
years on table 1 is a period in years. year in table is a date of when the account was created. so I have to find the difference from today's date to year on table2. then see if it matches any of the rows on table1. and return the results that match
This can be accomplished with a creative JOIN using date functions. Assuming table2.years is a DATE or DATETIME, use YEAR() to return only the year portion from it, and compare it to YEAR(CURDATE()) for the current year. Join that against the number of years in table1.year.
SELECT
table1.*,
table2.*
FROM
table1
JOIN table2
ON table1.year = (YEAR(CURDATE()) - YEAR(table2.years))