将mysql的时间与php输入时间进行比较

时间:2015-08-20 10:23:32

标签: php python mysql database raspberry-pi

在我的MySQL表callrecord中,行TIME_TAKEN包含解决问题的时间延迟。比如0:03:45,0:15:12等现在我想显示超过一定限度的所有记录,比如3分钟。我想通过网络浏览器输入此限制。但它无法比较延迟

我的数据库:

import MySQLdb
db= MySQLdb.connect("localhost","root","","testDB")
cursor=db.cursor()
cursor.execute("DROP TABLE IF EXISTS CALLRECORD")
#Create table as per requirement
sql="""CREATE TABLE CALLRECORD(
       DATE CHAR(12) NOT NULL,
       ROOM_NO CHAR(10) NOT NULL,
       BED_NO CHAR(10) NOT NULL,
       CALL_AT CHAR(13),
       SERVED_AT CHAR(13),
       TIME_TAKEN CHAR(13))"""
cursor.execute(sql)
#INSERT VALUES
sql="""INSERT INTO CALLRECORD(DATE,ROOM_NO,
       BED_NO,CALL_AT,SERVED_AT,TIME_TAKEN)
       VALUES(10/10/14,'1','1','10:45:12','10:57:32','0:12:20'),
         (10/10/15,'1','2','9:05:10','9:25:10','0:20:0')"""
try:
    #execute sql command
    cursor.execute(sql)
    #commit your changes into database
    db.commit()
except:
    #roleback if there is an error
    db.rollback()
#disconnect from server
db.close()

我的php程序是:

<html>    
<body>
<form method="post" action="searchcallhistory.php">
<input type="hidden" name="submitted" value="true" /> 

<label>Search Category:
<select name="category">
    <option value="ROOM_NO">ROOM</option>
    <option value="BED_NO">BED</option>
    <option value="DATE">DATE</option>
    <option value="TIME_TAKEN">DELAY</option>

</select>
</label>

<label>Search Criteria: <input type="text" name="criteria" /></label>

<input type="submit" />
</form>
<?php
if(isset($_POST['submitted'])) {
//connect to database
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$category=$_POST['category'];
$criteria=$_POST['criteria'];
if($catagory=='TIME_TAKEN')
{
$sqlget = "SELECT * FROM CALLRECORD WHERE CAST($category AS TIME)>=CAST('.$criteria.' AS TIME)";
}

else {
    $sqlget = "SELECT * FROM CALLRECORD WHERE $category LIKE '%".$criteria."%'";
}

mysql_select_db('testDB');
$sqldata = mysql_query($sqlget,$conn );
if(! $sqldata)
{
   echo ("error opening database"); 

   }
 $num_rows= mysql_num_rows($sqldata);
  echo "$num_rows results found";
  echo "<table>";
  echo"<tr><th>DATE </th><th > ROOM </th><th >BED</th><th>CALL AT</th>       <th>SERVED AT </th><th> TIME TAKEN</th></tr>";
  while($row = mysql_fetch_array($sqldata, MYSQL_ASSOC)) {
   echo "<tr><td>";
   echo $row['DATE'];
    echo "</td><td>";
echo $row['ROOM_NO'];
echo "</td><td>";
echo $row['BED_NO'];
echo "</td><td>";
echo $row['CALL_AT'];
echo "</td><td>";
echo $row['SERVED_AT'];
echo "</td><td>";
echo $row['TIME_TAKEN'];
echo "</td></tr>";
}
echo "</table>";
}
?>

</body>
</html>

除类别延迟外,它工作正常。

上有一些问题
$sqlget = "SELECT * FROM CALLRECORD WHERE CAST($category AS TIME)>=CAST('.$criteria.' AS TIME)";

显示所有的callrecord。没有按时间延迟过滤

如何解决它。???

0 个答案:

没有答案