Jump to content

Archived

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

Canman2005

SQL Query

Recommended Posts

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

[code]<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>[/code]

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

[code]<?
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 );

?>
[/code]

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

Share this post


Link to post
Share on other sites
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)"

Share this post


Link to post
Share on other sites
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) [snapback]350035[/snapback][/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

[code]<?
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);

?>[/code]

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

Share this post


Link to post
Share on other sites
[!--quoteo(post=350049:date=Feb 27 2006, 11:23 PM:name=Canman2005)--][div class=\'quotetop\']QUOTE(Canman2005 @ Feb 27 2006, 11:23 PM) [snapback]350049[/snapback][/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

[code]<?
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);

?>[/code]

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

Share this post


Link to post
Share on other sites

×

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.