hossfly007 Posted February 26, 2007 Share Posted February 26, 2007 I am trying to limit my db results down to the month. (1- Jan, 2-Feb, 3- Mar, etc.). When i use this code i cant pull anything. It's like i'm using the wrong DB query. help please <?php mysql_connect("XX", "XX", "XX") or die(mysql_error()); mysql_select_db("XX") or die(mysql_error()); if (is_numeric ($_GET['month']) ) { $getdata = "SELECT * FROM tours WHERE month={$_GET['month']}"; $data = mysql_fetch_array($getdata); print "Tours<br /><br />"; while ($data = mysql_fetch_array($getdata)) { print "{$data['td']}<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/ Share on other sites More sharing options...
nloding Posted February 26, 2007 Share Posted February 26, 2007 First, put all code between the CODE tags in the forum (the # symbol above the text box). Second, my guess is that you're using two different date formats. You have 1-Jan listed above, but what is the value of $_GET['month']? If it's not "1-Jan", that's it. Look into the date_format() functions for both PHP and MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194542 Share on other sites More sharing options...
hossfly007 Posted February 26, 2007 Author Share Posted February 26, 2007 www.mysite.com/tours.php?month=1 in the feild "month" the results are stored as: 1 One thing about it is i might have 20 results with the month as 1 Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194544 Share on other sites More sharing options...
nloding Posted February 26, 2007 Share Posted February 26, 2007 I don't put variables directly into my queries, personally. Try this, see if it makes a difference. If not, out of my league, on to the next guy! $getdata = "SELECT * FROM tours WHERE month=".$_GET['month']; Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194548 Share on other sites More sharing options...
artacus Posted February 26, 2007 Share Posted February 26, 2007 You can put variables directly into your queries, although using $_GET directly opens yourself to sql injection attacks. What you cant do is use quotes on your array keys inside of a double quoted string "... WHERE month = $_GET['month']" // no good "...WHERE month = $_GET[month]" // correct Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194550 Share on other sites More sharing options...
craygo Posted February 26, 2007 Share Posted February 26, 2007 well if you want to get a month you need to pull the month from the field then query it $getdata = "SELECT * FROM tours WHERE MONTH(month) = '".$_GET['month']."'"; That is if your form value is in number format Ray EDIT: actually that may be wrong. What format is the data in the month field??? Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194554 Share on other sites More sharing options...
hossfly007 Posted February 26, 2007 Author Share Posted February 26, 2007 when i put: print "$getdata"; it returns: SELECT * FROM tours WHERE month=1 That is working. It just wont pull all the results. (I've only used there WHERE statement to pull one result from my table, like an edit page) So i'm not certain that the query will pull ALL results that i have in my table that 1 is in the month field Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194558 Share on other sites More sharing options...
hossfly007 Posted February 26, 2007 Author Share Posted February 26, 2007 i'm actually kind of new at this so i'm not using field formats except text. i will manually enter the 1 in the feild and i know that 1 will mean january Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194560 Share on other sites More sharing options...
craygo Posted February 26, 2007 Share Posted February 26, 2007 well the query is working you have the code after it wrong <?php $getdata = "SELECT * FROM tours WHERE month={$_GET['month']}"; $result = mysql_query($getdata) or die(mysql_error()); print "Tours<br /><br />"; while ($data = mysql_fetch_array($result)) { print "{$data['td']}<br />"; } ?> Ray Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194562 Share on other sites More sharing options...
hossfly007 Posted February 26, 2007 Author Share Posted February 26, 2007 thanks everyone for helping me so fast, this is by far the best php forum out there ur tha man ray =] Quote Link to comment https://forums.phpfreaks.com/topic/40208-pulling-database-information-i-cant-limit-my-results/#findComment-194568 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.