【问题标题】:MYSQL select where timestamp data not retrieveMYSQL 选择不检索时间戳数据的位置
【发布时间】:2014-04-14 20:24:37
【问题描述】:

我的mysql表如下;

CREATE TABLE IF NOT EXISTS `cheque_data` (
`auto_no` int(11) NOT NULL AUTO_INCREMENT,
`job_no` int(11) NOT NULL,
`client_id` text NOT NULL,
`ch_no` text NOT NULL,
`ch_date` date NOT NULL,
`ch_bank` text NOT NULL,
`ch_amount` decimal(10,2) NOT NULL,
`sync` int(1) NOT NULL DEFAULT '0',
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`auto_no`)
 )

当我在 PHPMYADMIN 中运行 SQL 命令时,它将检索行;

SELECT * FROM `cheque_data` WHERE timestamp='2014-03-10 19:37:31'.

但在 PHP 中,

$sql = "select * from `cheque_data` where timestamp = '2014-03-10 19:37:31'";
$result_remote = mysqli_query($con,$sql) or die(mysqli_error($con));
$row_count = mysqli_num_rows($result_remote);
echo $row_count; //no data

它不提供任何数据。但是当我将时间戳更改为其他列时,它可以工作。

$sql = "select * from `cheque_data` where auto_no = '1'";
$result_remote = mysqli_query($con,$sql) or die(mysqli_error($con));
$row_count = mysqli_num_rows($result_remote);
echo $row_count; //data found

我想知道,这个问题的原因是什么? 它只是不适用于时间戳记

谢谢你, 萨梅拉

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    可能是您的PHP 脚本中的问题,而不是MySQL。 附言请记住,最好将列名包含在 ` 中,以免混淆 MySQL。在您的声明中没关系,但仅供参考,有一个名为TIMESTAMP() 的函数,在某些情况下,MySQL 可能会将未包装的column name 视为function name

    【讨论】:

      【解决方案2】:

      在时间戳字段周围添加反引号(`),因为它是保留字或使用此查询

      $sql = "select * from `cheque_data` where `timestamp` = '2014-03-10 19:37:31'";
      

      【讨论】:

      • 在这种情况下最好不要使用反引号
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 2011-10-19
      • 2013-10-18
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      相关资源
      最近更新 更多