Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the error in my PDO syntax

Tags:

php

mysql

pdo

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"
}
-->
like image 623
TecBrat Avatar asked Jun 01 '26 06:06

TecBrat


1 Answers

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();
like image 161
Sean Avatar answered Jun 03 '26 20:06

Sean