Jump to content

Dynamic Query String


DjMikeWatt

Recommended Posts

I have a table in a DB which has "year" and "month" among it's columns. These are indicators of when the item (record) was created and are set manually (varchar).

 

I want to query the DB and return only those results from the current month/year.  So, right now, for example, only those records that have "2009" in "year" and "July" in "month."  Right now I have:

$year = date('Y');
$month = date('F');
--------------------------------------------------------
mysql_select_db($database_imaging101, $imaging101);
$query_rs_downloads = "SELECT * FROM downloads, users WHERE `year` = $year AND `month` = $month AND `account_id` = users.id";
$rs_downloads = mysql_query($query_rs_downloads, $imaging101) or die(mysql_error());
$row_rs_downloads = mysql_fetch_assoc($rs_downloads);
$totalRows_rs_downloads = mysql_num_rows($rs_downloads);

When I test the page, the browser says "Unknown column 'July' in Where clause"

 

I've looked to see where I may have set the wrong variable in the Where clause, but I don't see where I've indicated $month as a column... what am I missing?

 

Any help is, as always, greatly appreciated!

Link to comment
Share on other sites

That's a fine question!

 

I don't really have an answer.  I suppose it's because I don't know what would be a better way.  That's not to say that there ISN'T a better way, I'm just self-taught and only know so much.

 

What would you suggest as a better way? I know the date function is extremely flexible, but I've always had a tough time with it...

Link to comment
Share on other sites

First of all, don't set the record creation time manually (if you actually do so, as you wrote earlier). Instead, use one single field where you store either a datetime or unix timestamp (faster). This will give you much freedom to do as you please with the data later on. You may still wish to store year and month in two separate fields but then use integers instead since it is both faster and more relevant.

Link to comment
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.