dreampho Posted March 2, 2012 Share Posted March 2, 2012 Hi. I am new to PHP and trying out database connections. I am trying to query the database, to see if the date matches a record in the field 'date'. If it does echo yes, if there are no matches 'no'. Can anyone tell me what is missing from my code: <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("date_picker", $con); $date= $_POST['date']; $new_date = date('Y-m-d',strtotime($date)); $sql = 'SELECT date FROM availability' if (date = $new_date) echo "Yes"; else echo "No"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/258103-anything-missing/ Share on other sites More sharing options...
Anon-e-mouse Posted March 2, 2012 Share Posted March 2, 2012 Well you've written the query for the table but you haven't actually executed it.. really the query will do nothing you want it to because you havent asked it to compare the date field with the date 'data' you have. Below! <?php $sql = "SELECT `date` FROM `availability` WHERE `date` = '".$date."'"; $get = mysql_query($sql); $pull = mysql_fetch_assoc($get); if($pull['date'] == $new_date){ echo 'Yes.'; } else { echo 'No.'; } ?> OR! <?php $sql = "SELECT `date` FROM `availability`"; $get = mysql_query($sql); $pull = mysql_fetch_assoc($get); while($pull = mysql_fetch_assoc($get)){ if($pull['date'] == $new_date){ echo 'Yes.'; } else { echo 'No.'; } } ?> The first will search for records that match the date and then go through the IF you wanted the second will run through ALL database records and then pair up the dates using the IF. Whenever you want to query a database you need to remember to actually execute it, its all well and good writing what you want to do (properly!) but if you don't then attempt to run the query it was a pointless effort: Write -> Execute (Or Error) -> Fetch results. Quote Link to comment https://forums.phpfreaks.com/topic/258103-anything-missing/#findComment-1323061 Share on other sites More sharing options...
Muddy_Funster Posted March 2, 2012 Share Posted March 2, 2012 also a word of advice : get the list of RESERVED WORDS that that your RDB uses and then don't use them for column, table or database names. Date is a reserved word, that's why Anon used backticks around it (well to be honest it looks as though Anon just wraps everything-and-its-dog in backticks whether it needs it or not, but that's besides the point) , this tells MySQL not to try to do anything with it. Also, as an FYI, Backticks are not single quotes and are not processed in the same way. Quote Link to comment https://forums.phpfreaks.com/topic/258103-anything-missing/#findComment-1323064 Share on other sites More sharing options...
dreampho Posted March 2, 2012 Author Share Posted March 2, 2012 Thank you to both of you. How would I redirect to a certain page for the if or else statements? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/258103-anything-missing/#findComment-1323070 Share on other sites More sharing options...
Anon-e-mouse Posted March 2, 2012 Share Posted March 2, 2012 Thank you to both of you. How would I redirect to a certain page for the if or else statements? Thanks Using the header function... <?php if($pull['date'] == $new_date){ header("Location: http://www.example.com"); } else { header("Location: http://www.example2.com"); } ?> Just make sure where you are redirecting to is valid of course and you should be ok. Quote Link to comment https://forums.phpfreaks.com/topic/258103-anything-missing/#findComment-1323071 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.