Reputation: 1091
I'm trying to execute a query one is working and one isn't
Code:
$sth = $this->_pdo->prepare("SELECT * FROM `messages` WHERE `service_id` = :service AND `created` > (NOW() - INTERVAL 7 DAY) LIMIT 1, :limit");
$leng = $this->_settings->length;
$sth->bindParam(":service", $this->_settings->service, PDO::PARAM_INT);
$sth->bindParam(":limit", $leng, PDO::PARAM_INT);
$sth->execute();
$component_data = $sth->fetchAll(PDO::FETCH_OBJ);
var_dump($component_data);
echo values : SELECT * FROM
messagesWHERE
service_id= 3 AND
created> (NOW() - INTERVAL 7 DAY) LIMIT 1, 5
this query isn't working
and this one is: SELECT * FROM
messagesWHERE
service_id= 2 AND
created> (NOW() - INTERVAL 7 DAY) LIMIT 1, 5
If I execute the first one inside my database editor tool I get this as result:
Why isn't the first query returning anything? and why is the second query returning something? even though they're the same?
Upvotes: 0
Views: 62
Reputation: 108370
I'm not spotting any specific error.
But the first suspect on the list is the bindParam
. For debugging, I'd try using a scalar, and also echo out the value.
$service_param = $this->_settings->service;
var_dump($service_param);
$sth->bindParam(":service", $this->_settings->service, PDO::PARAM_INT);
For debugging, I'd also try removing the bind placeholder for LIMIT, and hardcoding that in the statement.
Since there's no check of the return from the "prepare" and "execute" functions, so I'm going to assume that the PDO error mode is set to throw an exception if a database error occurs. That is, I'm assuming that the database connection has these attributes set:
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Upvotes: 1
Reputation: 1210
I spot various mistakes.
1: (deleted ignore)
2: Your limit clause should be 0,something. Putting a 1 there makes it skip 1 record.
Upvotes: 0