Stijn Bernards
Stijn Bernards

Reputation: 1091

PDO query returning 0

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 * FROMmessagesWHEREservice_id= 3 ANDcreated> (NOW() - INTERVAL 7 DAY) LIMIT 1, 5 this query isn't working

and this one is: SELECT * FROMmessagesWHEREservice_id= 2 ANDcreated> (NOW() - INTERVAL 7 DAY) LIMIT 1, 5

If I execute the first one inside my database editor tool I get this as result:

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

Answers (2)

spencer7593
spencer7593

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

Chuck
Chuck

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

Related Questions