Jump to content


Photo

SQL Query


  • Please log in to reply
3 replies to this topic

#1 Canman2005

Canman2005
  • Members
  • PipPipPip
  • Advanced Member
  • 669 posts

Posted 27 February 2006 - 10:41 PM

Hi all

I am wondering if you could help me, basically I have a html form which passes a value to a php page, the value is a date, such as 2001 or 2003, the form looks like

<form id="sqlfind" method="post" action="sqlfind.php">
<select name="date">
<option value="2010">2010</option>
<option value="2000">2000</option>
<option value="1990">1990</option>
</select>
<input name="Submit" type="submit" value="Search"></form>

the sql statement I use on the sqlfind.php page to get the results is

<?
session_start();
include ("db.php");

$sqlresult = "SELECT * FROM dates WHERE date=".$_POST['date']." AND live=1";

$dateresult = @mysql_query($sqlresult,$connection) or die(mysql_error());
$num = mysql_num_rows($dateresult );

?>

At the moment is brings results back for only rows which match the date, so if you select the year 1990 it will only find rows which contain 1990.

How could I get it to bring back the rows which contain the first 3 numbers of the year, if 1990 is selected it would look for 199 and if 2000 is selected it would look for 200.

For example, if you select 1990 from the drop down list on the form then the sql will return 1990 and also 1991 1992 1993 1994 1995 and so on. And if I selected 2000 from the drop down list on the form then it would bring back 2001 2002 2003 2004 2005.

Any help would be great.

Thanks in advance

Dave

#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 27 February 2006 - 10:49 PM

The best answer depends on what TYPE you've set date to.

$y3 = substr($_POST['date'], 0, 3);

If it's a string type:
"SELECT * FROM dates WHERE date LIKE '".$y3."%'"

If it's an int type
"SELECT * FROM dates WHERE date >= CAST ('".$y3."0' AS UNSIGNED) AND date <= CAST ('".$y3."9' AS UNSIGNED)"

If it's a date type
"SELECT * FROM dates WHERE date >= CAST ('".$y3."0' AS DATE) AND date <= CAST ('".$y3."9' AS DATE)"

#3 Canman2005

Canman2005
  • Members
  • PipPipPip
  • Advanced Member
  • 669 posts

Posted 27 February 2006 - 11:23 PM

Hello

thanks for that, it worked great. I have another question about the same form and php page.

How could we get the sql statement to return all rows which contain the first 3 numbers of whatever has been selected from the drop down list?

So if 1990 is selected from the drop down on the form, it would return all rows which have 199 in the date column, so 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 as they all start with 199

Any help would be great

Thanks

Dave


[!--quoteo(post=350035:date=Feb 27 2006, 10:49 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Feb 27 2006, 10:49 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The best answer depends on what TYPE you've set date to.

$y3 = substr($_POST['date'], 0, 3);

If it's a string type:
"SELECT * FROM dates WHERE date LIKE '".$y3."%'"

If it's an int type
"SELECT * FROM dates WHERE date >= CAST ('".$y3."0' AS UNSIGNED) AND date <= CAST ('".$y3."9' AS UNSIGNED)"

If it's a date type
"SELECT * FROM dates WHERE date >= CAST ('".$y3."0' AS DATE) AND date <= CAST ('".$y3."9' AS DATE)"
[/quote]


Hi

Date is a int

I have put the following but it didnt like it

<?
session_start();
include ("db.php");
$y3 = substr($_POST['year'], 0, 3);
$sql ="SELECT * FROM dates WHERE date >= CAST ('".$y3."0' AS UNSIGNED) AND date <= CAST ('".$y3."9' AS UNSIGNED)";

$result = @mysql_query($sql,$connection) or die(mysql_error());
$num = mysql_num_rows($result);

?>

I get the error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('2010' AS UNSIGNED) AND date <= CAST ('2019' AS UNSIGNED)' at line 1

Any ideas?

Thanks

Dave

#4 Canman2005

Canman2005
  • Members
  • PipPipPip
  • Advanced Member
  • 669 posts

Posted 27 February 2006 - 11:37 PM

[!--quoteo(post=350049:date=Feb 27 2006, 11:23 PM:name=Canman2005)--][div class=\'quotetop\']QUOTE(Canman2005 @ Feb 27 2006, 11:23 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hello

thanks for that, it worked great. I have another question about the same form and php page.

How could we get the sql statement to return all rows which contain the first 3 numbers of whatever has been selected from the drop down list?

So if 1990 is selected from the drop down on the form, it would return all rows which have 199 in the date column, so 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 as they all start with 199

Any help would be great

Thanks

Dave
Hi

Date is a int

I have put the following but it didnt like it

<?
session_start();
include ("db.php");
$y3 = substr($_POST['year'], 0, 3);
$sql ="SELECT * FROM dates WHERE date >= CAST ('".$y3."0' AS UNSIGNED) AND date <= CAST ('".$y3."9' AS UNSIGNED)";

$result = @mysql_query($sql,$connection) or die(mysql_error());
$num = mysql_num_rows($result);

?>

I get the error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('2010' AS UNSIGNED) AND date <= CAST ('2019' AS UNSIGNED)' at line 1

Any ideas?

Thanks

Dave
[/quote]

Your other idea worked great. thanks. all fixed

stay well

dave





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users