DjMikeWatt Posted July 27, 2009 Share Posted July 27, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/167570-dynamic-query-string/ Share on other sites More sharing options...
DjMikeWatt Posted July 27, 2009 Author Share Posted July 27, 2009 ADDITION: I think I may have fixed the problem by adding single quotes around the $month/$year variables in Where clause... could that have been the issue? Quote Link to comment https://forums.phpfreaks.com/topic/167570-dynamic-query-string/#findComment-883672 Share on other sites More sharing options...
mmarif4u Posted July 27, 2009 Share Posted July 27, 2009 Single quote needed around the variables in query. Quote Link to comment https://forums.phpfreaks.com/topic/167570-dynamic-query-string/#findComment-883674 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 Single quote needed around the variables in query. Exactly. Just one question, why do you store these variables manually and why as varchars? That doesn't seem like a good solution. Quote Link to comment https://forums.phpfreaks.com/topic/167570-dynamic-query-string/#findComment-883675 Share on other sites More sharing options...
DjMikeWatt Posted July 27, 2009 Author Share Posted July 27, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/167570-dynamic-query-string/#findComment-883679 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167570-dynamic-query-string/#findComment-883696 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.