Jump to content

Anything missing?


dreampho

Recommended Posts

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)

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.