Canman2005 Posted February 27, 2006 Share Posted February 27, 2006 Hi allI 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 advanceDave Quote Link to comment https://forums.phpfreaks.com/topic/3710-sql-query/ Share on other sites More sharing options...
wickning1 Posted February 27, 2006 Share Posted February 27, 2006 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 Link to comment https://forums.phpfreaks.com/topic/3710-sql-query/#findComment-12871 Share on other sites More sharing options...
Canman2005 Posted February 27, 2006 Author Share Posted February 27, 2006 Hellothanks 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 199Any help would be greatThanksDave[!--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]HiDate is a intI 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 errorYou 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 1Any ideas?ThanksDave Quote Link to comment https://forums.phpfreaks.com/topic/3710-sql-query/#findComment-12880 Share on other sites More sharing options...
Canman2005 Posted February 27, 2006 Author Share Posted February 27, 2006 [!--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--]Hellothanks 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 199Any help would be greatThanksDaveHiDate is a intI 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 errorYou 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 1Any ideas?ThanksDave[/quote]Your other idea worked great. thanks. all fixedstay welldave Quote Link to comment https://forums.phpfreaks.com/topic/3710-sql-query/#findComment-12890 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.