Jump to content

Recommended Posts

I have a table in the database named posted in the date format 0000-00-00

 

Is this possible to do?

<?php
$month = date("m");

$query = "SELECT * FROM charts WHERE username = 'admin' AND posted = '".date("m", strtotime($month))."'";
				$chart = mysql_query($query);
                                        $numorws = mysql_num_rows($chart);
					if($numrows == 0){
						echo "nothing";
					}
				while($charts = mysql_fetch_assoc($chart)){
				echo $charts['id']."<br />";
				}

?>

 

It is returning "nothing", even though there are two items in the db with the posted being

2009-01-14

2009-01-19

Link to comment
https://forums.phpfreaks.com/topic/141470-solved-select-query/
Share on other sites

neil.johnson is correct you are using strtotime for no reason.

 

You also are using the wrong date parameter to set the month var.  Use

 

$month = date('n'); #this is the equivalent to mysql month()

$query = "SELECT * FROM charts WHERE username = 'admin' AND month(posted) = ".$month;

 

 

Link to comment
https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740540
Share on other sites

phparray,

I'm using the correct var for the month

<?php
date("m");  #numerical representation of month WITH leading zeros
date("n");  #numerical representation of month WITHOUT leading zeros
?>

 

And if you look at my first post, there are leading zeros

 

Thanks Neil for the help

 

Link to comment
https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740548
Share on other sites

date("n");  #numerical representation of month WITHOUT leading zeros

And if you look at my first post, there are leading zeros

 

Exactly - using the mysql function month() will output from a date field WITHOUT leading zeros.  So using the php function date('n') will be a direct comparison to month(posted) on your table. 

Link to comment
https://forums.phpfreaks.com/topic/141470-solved-select-query/#findComment-740554
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.