简体   繁体   中英

PHP SELECT WHERE DATETIME AND TEXT

$result = $this->database->query(printf("SELECT * FROM 'events' WHERE ('date_time' = \"%s %s\" AND 'title' = \"%s\")", $date, $time, $title));

Results in:

SELECT * FROM 'events' WHERE ('date_time' = "2013-12-06 18:00:00" AND 'title' = "Viikate, The Mutants");SQLSTATE[HY000]: General error: 1 near "106": syntax error

Tried with ; in query and without, with parenthesis in query and without.

You are using single quote ' around your table column names which is not correct. Wrap your column names around backticks or do not wrap them at all. I prefer using backticks ` to separate the column names.

$result = $this->database->query(printf("SELECT * FROM `events` WHERE (`date_time` = \"%s %s\" AND `title` = \"%s\")", $date, $time, $title));

You shouldn't put ' around table and column names. Either use ` or nothing at all.

Use sprintf , printf is function to console out. And your query also needs to be modified to use backtick(`) to enclose table or field name.

ie)

SELECT * FROM \`events\` WHERE (\`date_time\` = \"%s %s\" AND \`title\` = \"%s\")

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