fluentpdo icon indicating copy to clipboard operation
fluentpdo copied to clipboard

Error with %d format symbol inside select clause

Open KarelWintersky opened this issue 3 years ago • 3 comments

$dbh = new \PDO(sprintf("mysql:host=%s;port=%s;dbname=%s", 'localhost', 3306, 'database'), 'root', 'password');

$query = new \Envms\FluentPDO\Query($dbh);
$query = $query;

$query = $query
  ->from("articles")
  ->select("
        *,
        DATE_FORMAT(cdate, '%H:%i %d.%m.%Y') AS cdate2,
        DATE_FORMAT(cdate, '%d.%m.%Y') AS cdate3,
        ", true
    );

var_dump($query->getQuery());

Result is:

SELECT
        *,
        DATE_FORMAT(cdate, '%H.%i %m.%Y') AS cdate2,
        DATE_FORMAT(cdate, %m.%Y') AS cdate3,
FROM articles
    LEFT JOIN H ON H.articles_id = articles.id
    LEFT JOIN d ON d.id = articles.d_id

WTF? O.O

KarelWintersky avatar Feb 07 '22 13:02 KarelWintersky

([^[:space:]()]+[.:])[\p{L}\p{N}\p{Pc}\p{Pd}\p{Pf}\p{Pi}]* error somewhere here...

KarelWintersky avatar Feb 07 '22 14:02 KarelWintersky

Thanks for submitting @KarelWintersky, I had a chance to look at this now. It's very likely that you're using a dot . as the time separator. There are a couple ways to solve what you're attempting:

  1. Use the Literal class (eg. new Literal('%H.%i')) built into Fluent to prevent automatically attempting joins on those values.
  2. Use another separator other than . and :.

cbornhoft avatar Feb 13 '22 19:02 cbornhoft

@cbornhoft is any way to remove : from params (named placeholder) and insert it automatically?

KarelWintersky avatar Feb 14 '22 14:02 KarelWintersky