查找日期 mysql 前 2 天的数据和 fat free 框架

Find data 2 days before date mysql and fat free framework

嗨,我正在尝试制作即将推出的服务的模块,我需要在日期前 2 天或更多天向用户展示服务,我正在使用 php fat free 和 mysql 我有这个查询:

SELECT * FROM bitacora WHERE fechaprox >= NOW() - INTERVAL 2 DAY; 

这适用于 mysql enter image description here

我正在尝试像这样将其脱脂:

public function avisos($f3)
{
    $this->M_Bitacora->cliente = 'SELECT nombre FROM cliente WHERE id_cliente= bitacora.id_cliente';
   $result= $this->M_Bitacora->find('SELECT * FROM bitacora WHERE fechaprox >= NOW() - INTERVAL 2 DAY');
   $items= array();
   foreach($result as $bitacora){
       $items[] = $bitacora->cast();
   }
   echo json_encode([
    'mensaje' => count($items) > 0 ? '' : 'Aun no hay registros',
    'info'=> [
        'items' => $items,
        'total' => count($items)
    ]
]);
    
}

但这是我的错误:内部服务器错误

PDOStatement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM bitacora WHERE fechaprox >= NOW() - INTERVAL 2 DAY' at line 1

你能帮帮我吗?还是有另一种方法来获取我需要的数据?

所以我要大胆地说 M_Bitacora 是一个 DB\SQL\Mapper 对象,对吗?

如果您使用的是映射器对象,那么您所做的就是使用映射器对象来隐藏所有讨厌的东西 SQL 并将其隐藏在其他地方。它有一堆辅助方法 find()load()select() 以及其他方法来快速帮助您获取所需的数据并将其映射到 PHP 对象。

由于您要走原始 SQL 路线,您需要的是使用您的数据库连接。如果您仍想使用 SQL,那么最好将您创建的 SQL 连接变量设为 运行 SQL。然后像@Caius Jard 建议的那样,使用 exec().

<?php

// if this is how you set up your db connection...
$f3->set('DB', new DB\SQL(/*config stuff*/);

// then in your code example above
public function avisos($f3)
{
    // you can just join this in the query below. Much more efficient anyways.
    //$this->M_Bitacora->cliente = 'SELECT nombre FROM cliente WHERE id_cliente= bitacora.id_cliente';
   $items= $f3->DB->exec('SELECT b.*, c.nombre 
        FROM bitacora b 
        JOIN cliente c ON c.id_cliente = b.id_cliente
        WHERE b.fechaprox >= NOW() - INTERVAL 2 DAY');

   // it is automatically fetch as an associative array (hopefully if you configured your PDO object like that)
   //$items= array();
   //foreach($result as $bitacora){
   //    $items[] = $bitacora->cast();
   //}
   echo json_encode([
    'mensaje' => count($items) > 0 ? '' : 'Aun no hay registros',
    'info'=> [
        'items' => $items,
        'total' => count($items)
    ]
]);
    
}

请再次检查 documentationfindfilter 作为参数,而不是整个 SELECT 语句。

public function avisos($f3)
{
   $result= $this->M_Bitacora->find(['fechaprox >= NOW() - INTERVAL 2 DAY']);
   $items = array_map(function($e) { return $e->cast(); }, $result);
   echo json_encode([
    'mensaje' => count($items) > 0 ? '' : 'Aun no hay registros',
    'info'=> [
        'items' => $items,
        'total' => count($items)
    ]
]);
    
}