Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble using PDO for the first time

Tags:

php

mysql

pdo

I'm trying to get started with PDO and am having some trouble. Here's my original code:

    $query = "
              UPDATE `products` 
              SET `product_qty` = '{$_GET['product_qty']}'
              WHERE `product_id` = '{$_GET['product_id']}'
    ";

    mysql_query($query) or die(mysql_error());

That works fine, but when I try to translate that to PDO syntax:

    $db->prepare('
    UPDATE products 
    SET product_qty = :product_qty
    WHERE product_id = :product_id
    ');

    try 
    {
        $db->execute(array(':product_qty' => $_GET['product_qty'], ':product_id' => $_GET['product_id']));
    }
    catch (PDOException $e) 
    {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }

I get the error:

Fatal error: Call to undefined method PDO::execute() in ...


Could someone help me get my first PDO query working?

like image 393
Nate Avatar asked Jul 04 '12 16:07

Nate


People also ask

How do you write PDO?

$pdo = new PDO($dsn, $user, $passwd); A new PDO object is created. We pass the constructor the data source name and the user name and password. The PDO class represents a connection between PHP and a database server.

Which PDO method is used to prepare a statement for execution?

Description ¶ Prepares an SQL statement to be executed by the PDOStatement::execute() method. The statement template can contain zero or more named (:name) or question mark (?) parameter markers for which real values will be substituted when the statement is executed.

How does PHP PDO work?

PDO—PHP Data Objects—are a database access layer providing a uniform method of access to multiple databases. It doesn't account for database-specific syntax, but can allow for the process of switching databases and platforms to be fairly painless, simply by switching the connection string in many instances.


4 Answers

The $db->prepare() returns a PDOStatement which has the execute() method.

$stmt = $db->prepare('UPDATE products 
    SET product_qty = :product_qty
    WHERE product_id = :product_id');

$stmt->execute(array(
    ':product_qty' => $_GET['product_qty'], 
    ':product_id' => $_GET['product_id']
));
like image 164
Ja͢ck Avatar answered Oct 06 '22 01:10

Ja͢ck


$db->prepare() returns a PDOStatement object. You need to call execute() on that, not on $db.

like image 34
Maerlyn Avatar answered Oct 06 '22 02:10

Maerlyn


I refer ya to the example... the prepare creates a statement, and it's that that you run execute() on...

<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>
like image 38
Brian Avatar answered Oct 06 '22 00:10

Brian


The call to prepare returns a PDOStatement, it is this that you need to execute. Try the following:

$sth = $db->prepare('
  UPDATE products 
  SET product_qty = :product_qty
  WHERE product_id = :product_id
');

$sth->execute(array(':product_qty' => $_GET['product_qty'], ':product_id' => $_GET['product_id']));
like image 33
Rich O'Kelly Avatar answered Oct 06 '22 01:10

Rich O'Kelly