简体   繁体   中英

I want to fetch time column from MySQL database

I want to fetch time column from mysql database and to compare with current time. It should execute only if it matches with the current time. I am new to php. This is my Table

What I tried is

 <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} 
$today = date("h:i a")
$sql = "SELECT * FROM Table_name WHERE time = $today";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // execute you code
} else {

}

$conn->close();
?> 

But the problem is in my table hyphen and 'or' symbol exists, I dont know how to compare?? Is it possible to compare the time like this format??

Datatype for timings column is longtext

For this you need to simply change the way for time storing in the database. Store the time in UNIX TIMESTAMP. I have worked with your code.

mysql to insert as UNIX TIMESTAMP

$sql = "INSERT into `Table_name` set time = time()";

Your code with modification in $today = time();

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} 

$today = time();

$sql = "SELECT * FROM Table_name WHERE time = '".$today."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // execute you code
} else {

}

$conn->close();
?> 

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