0

I've got a table with some columns. I want to filter some records using two of them, the one with INT type and second with DATETIME type. I'm using PHP PDO extension to connect with database and make some queries.

I'm trying to get the records from my table where datetime field is lower then given date, f.e.

<?php
    $date = date("Y-m-d");
    $this->db->query("SELECT * FROM `" . DB_PREFIX . "fanpage` WHERE `flag_warning` = ? AND DATE(`update_date`) < ?", array(1, $date));
?>

This returns NULL, but when I paste the same query into the phpMyAdmin window it shows me proper records. What is the problem?

Edit:

Fragment from query function:

public function query($sql, $params = array())
{
    $result = array();
    $result['query'] = $this->pdo->prepare($sql);
    $result['query']->execute($params);
    $this->lastResult = $result['query'];
    unset($result['query']);
}
3
  • well can you show the actual prepare method in your library because this doesn't mean anything
    – DevZer0
    Aug 7, 2013 at 10:49
  • Did you call $this->db->execute() ?
    – Jithesh
    Aug 7, 2013 at 10:52
  • My prepare is an extension of PDO::prepare function.
    – Kamil
    Aug 7, 2013 at 10:58

3 Answers 3

0

No need for the prepared statements at all

WHERE flag_warning = 1 AND update_date < CURDATE()
4
  • Also, when I remove the update_date < CURDATE() I get the results
    – Kamil
    Aug 7, 2013 at 10:59
  • @CarlosCampderrós - timestamp
    – Kamil
    Aug 7, 2013 at 11:15
  • Also curious thing is that when I use the same query on different table (but same columns) it works. Maybe some table settings or something?
    – Kamil
    Aug 7, 2013 at 11:37
  • everything you are curious of can be easily found out: just SELECT valuest you are trying to use in WHERE clause and see Aug 7, 2013 at 11:46
0

Use

$sqlStatement = $this->db->prepare("SELECT * FROM `" . DB_PREFIX . "fanpage` WHERE `flag_warning` = ? AND `update_date` < CURDATE()");
$this->db->execute(array(1));
$result = $sqlStatement->fetchAll(PDO::FETCH_ASSOC);

Now $result has what you need.

1
  • Still not showing proper result, each time I get nothing.. When I try that query in phpMyAdmin or SQL console it shows me results..
    – Kamil
    Aug 7, 2013 at 11:15
-1

I've changed the column name to date_time_upd and it works right now, I think it's bug or something, maybe someone can explain that?

Edit:

Okay, I've figured it out. There was a fragment of code that checked for occurrence of the "UPDATE, DELETE OR INSERT" word in the query, and if there was a word like that the query result was not fetched. I've changed that to search for SELECT word, now everything is okay.

3
  • that's why these fragment checking facilities are evil Aug 7, 2013 at 11:47
  • that's true, I know that right now, do you know any better choice?
    – Kamil
    Aug 7, 2013 at 11:50
  • yes, sure. To use Object oriented programming instead of trying to use one single function to do all the database operations in one call based on some unreliable magic. Aug 7, 2013 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.