Jump to content

Pulling Database Information - "I Can't Limit my Results"


hossfly007

Recommended Posts

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 :D

 

 

 

 

 

 

<?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 />";
}
}
?>

 

 

 

 

 

 

 

 

 

 

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.

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

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???

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

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.