简体   繁体   中英

Only first of multiple PDO queries being executed?

I'm attempting to execute two separate PDO queries in a row. However, only one will execute correctly at a time. Whichever one comes first is the only one to correctly query the database and return any values. I've tried using var_dump to see what they're returning, and the second PDO query returns "boolean false" and doesn't contain any information.

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

// make and execute query for client information
    $client_query = $db->prepare('SELECT * FROM clients WHERE id=:ID');
    $client_query->execute(array('ID' => $client_id));
    $client_result = $client_query->fetch(PDO::FETCH_ASSOC);

    // set client information for captcha
    $this->client_name = $client_result['name'];
    $this->client_id = $client_result['id'];

    $client_query->closeCursor();

    // make and execute query for campaign information
    $campaign_query = $db->prepare('SELECT * FROM campaigns WHERE id=:ID AND client_id=:CLIENT');
    $campaign_query->execute(array('ID' => $campaign_id, 'CLIENT' => $client_id));
    $campaign_result = $campaign_query->fetch(PDO::FETCH_OBJ);

    // set campaign information for captcha
    $this->campaign = $campaign_result;

If I remove the parameters from the second query, they both execute successfully. What am I doing wrong here? Thanks!

Your SQL is invalid; change this:

SELECT * FROM campaigns WHERE id=:ID AND WHERE client_id=:CLIENT

to this:

SELECT * FROM campaigns WHERE id=:ID AND client_id=:CLIENT

As the Errors and error handling chapter explains:

PDO offers you a choice of 3 different error handling strategies

... and the default is PDO::ERRMODE_SILENT , that means: suppress all error messages and hope the developer doesn't forget to check if there were errors.

To sum up: you need to do error checking.

At a first glance... I would change your code to: (Use colons in the arrays as well and there were duplicates of the reseverd keyword "where" in the second sql-statement). I would exception instead of warning- and then catch exception when necessary.

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// make and execute query for client information
$client_query = $db->prepare('SELECT * FROM clients WHERE id=:ID');
$client_query->execute(array(':ID' => $client_id));
$client_result = $client_query->fetch(PDO::FETCH_ASSOC);

// set client information for captcha
$this->client_name = $client_result['name'];
$this->client_id = $client_result['id'];

$client_query->closeCursor();

// make and execute query for campaign information
$campaign_query = $db->prepare('SELECT * FROM campaigns WHERE id=:ID AND client_id=:CLIENT');
$campaign_query->execute(array(':ID' => $campaign_id, ':CLIENT' => $client_id));
$campaign_result = $campaign_query->fetch(PDO::FETCH_OBJ);

// set campaign information for captcha
$this->campaign = $campaign_result;

You could do like above when trying to retrieve values, BUT consider using JOINS instead like this:

SELECT * FROM clients cls
LEFT JOIN campaigns cas ON (cls.id = cas.client_id)
WHERE cas.id=:ID

Embarrassingly, all the code was correct (except for a small SQL syntax mistake that some of you caught). My problem was that the value for $campaign_id was greater than any values stored in the mySQL table, and so the query actually returned no results. So actually there was no problem with the queries, there were just no results to return for the second one. I appreciate all the answers and apologize for this sad oversight!

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