3

Sorry I couldn't come up with a better title. :-(

This is my first time trying to use PDO. I have referenced php.net for syntax. The database is empty and looks like this:

Field Type Null Key Default Extra id int(11) NO PRI NULL auto_increment query_string text NO NULL exclude_count tinyint(4) NO NULL 

Running the code should create or update a row in the pages table, the DB is unchanged.

Here's my code and output:

$dbuser='abc'; $dbpass='password'; $connection = new PDO('mysql:host=127.0.0.1;dbname=mydb', $dbuser, $dbpass); if(!$connection){echo '<!-- DB CONNECTION ERROR -->';}else{echo '<!-- DB CONNECTION CONFIRMED -->';} $connection->beginTransaction(); $prep_idcheck=$connection->prepare("SELECT id FROM pages WHERE query_string = :origqs"); $prep_idcheck->bindParam(':origqs', $orig_QS); $row=$prep_idcheck->fetch(); if($row) { echo '<!-- EXDB: We have dealt with this one before. -->'; $existing=true; } else { echo '<!-- EXDB: This is a new one. -->'; $existing=false; } if($existing) { $prep_report_excounts=$connection->prepare("UPDATE pages SET exclude_count = :exclude_count WHERE query_string = :origqs"); } else { $prep_report_excounts=$connection->prepare("INSERT INTO pages (exclude_count, query_string) VALUES (:exclude_count,:origqs)"); } $prep_report_excounts->bindParam(':origqs', $orig_QS); $prep_report_excounts->bindParam(':exclude_count', $exclude_count); $status=$prep_report_excounts->execute(); if(!$status) { $error=$connection->errorInfo(); echo'<!-- The statement did not execute '."\n"; var_dump($error); echo'-->'; } $connection->commit(); ?> 

Output

<!-- DB CONNECTION CONFIRMED --><!-- EXDB: This is a new one. --><!-- The statement did not execute array(1) { [0]=> string(5) "00000" } --> 
3
  • 2
    You do not call $prep_idcheck->execute(); Commented May 2, 2013 at 20:47
  • Thanks, I see that now. I'll fix that. I also realized my $orig_QS & $exclude_count variables are not set and I was SURE they were. That's probably the bigger part of my problem. Commented May 2, 2013 at 20:53
  • It works now, I did add the execute() to the idcheck, but problem I was noticing was caused by empty variables being bound as parameters. Commented May 3, 2013 at 20:04

2 Answers 2

5

You are missing the execute() on $prep_idcheck

$prep_idcheck=$connection->prepare("SELECT id FROM pages WHERE query_string = :origqs"); $prep_idcheck->bindParam(':origqs', $orig_QS); // Missing execute(); $prep_idcheck->execute(); $row=$prep_idcheck->fetch(); 
Sign up to request clarification or add additional context in comments.

Comments

4

You did not execute your statement

$prep_idcheck->bindParam(':origqs', $orig_QS); $prep_idcheck->execute(); // <------ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.