简体   繁体   中英

Matching date and time from mysql - logic issue

I have uploaded a PHP script in the server. What it does is, that it keeps reading the DB (mySQL), and if the DATE_OF_MATCH and TIME_OF_MATCH (these are 2 fields in the mySQL db) is equal to the server time it will execute a message.

fields in the table: all are VARCHAR

ID, DATE_OF_MATCH, TIME_OF_MATCH, MATCH_NAME

One record from the MATCH table;

1 , 1/12/2012, 3:40, ManU vs Kaks

The problem is that, my select statement is wrong. My $theDateAndTime is returning 09:15:03PM and in the Database i am having 2 separate records for date and time. So how can i edit the select statement so could match the date and time against the $theDateAndTime (returned by the server)

The code:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);
date_default_timezone_set('America/New_York');
$theDateAndTime = date("h:i:sA")."\n";



$result = mysql_query("SELECT * FROM MATCH where DATE_OF_MATCH=".$theDateAndTime." and TIME_OF_MATCH=".$theDateAndTime."");

while(true){
if(result!=null){

while($row = mysql_fetch_array($result))
  {
  echo $row['MATCH_NAME'] ;
  echo "<br />";
  }
}
}

mysql_close($con);
?>

I don't understand your question very well, but seems that you are trying to query for date and time in two different fields but you are only take the current time. I think you should try something like this:

<?php
$current_date = date('d/m/Y');
$current_time = date('h:i');

$result = mysql_query("SELECT * FROM MATCH where DATE_OF_MATCH=".$current_date." and TIME_OF_MATCH=".$current_time."");

Otherwise, you should not use mysql_* functions, use mysqli_* functions instead with prepared statements.

Create two separate variables for date and then time.

$the_date = date("n/j/Y");
$the_time = date("h:i:s");

$result = mysql_query("SELECT * FROM MATCH where DATE_OF_MATCH={$the_date} and TIME_OF_MATCH={$the_time}");

http://us3.php.net/manual/en/function.date.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM