Help w/pdo

Hello, can someone help with this code.

Fatal error: Call to undefined method PDO::execute() in C:\xampp\htdocs\invoice\ axform.php on line 136

<?php

$db = new PDO(
	'mysql:dbname=homedb;hostname=localhost',
	'root',	'cookie'
);
$db->prepare('
	INSERT INTO numbers (
		taxrate
	) VALUES (
		:taxRate
	)
');
$db->execute(array(	':taxrate' => $rate )); // error points to this
template_header('Tax Rate ' . (
	$outputRate = htmlspecialchars($_POST['taxRate'])
) . ' Set');
echo '		
<p>
			Rate successfully set to ',$outputRate, '
		</p>';		
template_footer();
die;
?>

Assuming username and password are correct, try

$db = new PDO("mysql:host=localhost;dbname=homedb", "root", "cookie");

Hi,

The problem is that you’re calling execute() on the PDO object. The prepare() method returns a PDOStatement object, and you have to call execute() on that:


$stmt = $db->prepare('
    INSERT INTO numbers (
        taxrate
    ) VALUES (
        :taxRate
    )
');
$stmt->execute(array(':taxrate' => $rate));

http://www.php.net/pdo.prepared-statements

I’m sorry but I still get:

Notice: Undefined variable: pdo in C:\xampp\htdocs\invoice\ axform.php on line 139
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\invoice\ axform.php on line 139

<?php
// Create the database connection as a PDO object:  
  try 
  
  {$db_options = array(
   PDO::ATTR_EMULATE_PREPARES => false  // important! use actual prepared statements (default: emulate prepared statements)
   , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent)
   , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC  // fetch associative arrays (default: mixed arrays)
    ); 
$db = new PDO('mysql:host=localhost;dbname=homedb;charset=utf8', 'root', 'cookie');}

 catch (PDOException $e)
 
 { // Report the Error! 
 echo "Something is not right, check your php.ini settings or code<br>";}
 
$query = 'UPDATE  numbers SET taxrate = :taxrate WHERE id = :id'; 
 // Prepare the Statement:
 $stmt = $pdo->prepare($query); 
 // execute the statement: 
$result = $stmt->execute(array(':taxrate' => $rate, ':id' => $id)); // primary key in the table column.
  
  if ($result) { echo "Success!"; }
  
  else { echo "Not again"; }  // Failure 
  
?>

That’s because in this code you’ve just posted you’ve assigned the PDO connection to the variable $db and then lower down you’re trying to call [B]$pdo[/B]->prepare instead of [B]$db[/B]->prepare

Edit: I also notice that you’re defining an options array, but you’re not actually passing it to the PDO constructor. You need to do this:

$db = new PDO('mysql:host=localhost;dbname=homedb;charset=utf8', 'root', 'cookie', $db_options);

I get:

Notice: Undefined variable: rate in C:\xampp\htdocs\invoice\ axform.php on line 138
Notice: Undefined variable: id in C:\xampp\htdocs\invoice\ axform.php on line 138
Success!

with this code:

<?php
    // Create the database connection as a PDO object:  
      try 
      
      { $db_options = array(
       PDO::ATTR_EMULATE_PREPARES => false  // important! use actual prepared statements (default: emulate prepared statements)
       , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent)
       , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC  // fetch associative arrays (default: mixed arrays)
        ); 
$db = new PDO('mysql:host=localhost;dbname=homedb;charset=utf8', 'root', 'cookie', $db_options); }
     
catch (PDOException $e)     
     { // Report the Error! 
     echo "Something is not right, check your php.ini settings or code<br>";}  
   
    $query = 'UPDATE  numbers SET taxrate = :taxrate WHERE id = :id'; 
     // Prepare the Statement:
     $stmt = $db->prepare($query); 
     // execute the statement: 
    $result = $stmt->execute(array(':taxrate' => $rate, ':id' => $id)); // primary key in the table column.
            
if ($result) { echo "Success!"; } 
     
      else { echo "Not again"; }  // Failure       
    ?>

You aren’t defining the variables $id or $rate anywhere in the code you posted, but you’re trying to use them here:

$result = $stmt->execute(array(':taxrate' => $rate, ':id' => $id));

thanks

Only driver specific (i.e. mysql specific) options can be passed in the constructor. To set thing like the error handler requires using the setAttribute method. Just one of those things.