Jump to content

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


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

 

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.