PhpDude
PhpDude

Reputation: 1598

Selecting rows from current month mysqli & php

I have a cell in my database which timestamps a task once complete, for reporting I am wanting to show the figure of row which result in the current month.

So for example there may be 10 tasks complete this month but I need to show that figure, I am trying to do this with the following but not even getting any output from it?

$sql="SELECT published FROM to_do_list WHERE MONTH(date)=MONTH(NOW())";
$result = mysqli_query($db, $sql);

if ($result=mysqli_query($db,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);
  printf("Result set has %d rows.\n",$rowcount);
  // Free result set
  mysqli_free_result($result);
  }

So there is a cell called published which is a DATETIME. the aim is to count and show how many rows contain the current month from that.

Upvotes: 0

Views: 826

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

Firstly, you're using mysqli_query() twice and that should be throwing you an error about it:

So just do:

if ($result=mysqli_query($db,$sql))

and get rid of the line above that and you'll be good to go

Plus, as stated by the OP in comments after checking for errors:

"Well that did it! Forgot to change date to published in my query"

As per another comment by one of Stack's great members:

"You might want to add that adding a check for the year might make the results a bit more reliable :-) – jeroen"

Upvotes: 2

Related Questions