1

i want run a query in a insert statement in pdo when i bind my value with prepare and bindValue functions.

i want run a code like this in pdo:

INSERT INTO test_table1 (`item1`, `item2`, `item3`) 
VALUES (
        'value of item1', 
        'value of item2', 
        (SELECT `value_of_item3` FROM test_table2 WHERE `item_id` = '3' LIMIT 1)
)

in mysql and in pdo when i don't bind my value, it work very well but in pdo when i bind my value like bellow code it doesn't work.

$sth = $db->prepare("INSERT INTO MyGuests (`item1`, `item2`, `item3`)
    VALUES (:item1, :item2, :item3)");
$sth->bindValue(':item1', 'value of item1');
$sth->bindValue(':item2', 'value of item2';
$sth->bindValue(':item3', '(SELECT `value_of_item3` FROM test_table2 WHERE `item_id` = '3' LIMIT 1)');
$sth->execute();

Someone have an idea what to do?

Morteza
  • 41
  • 4
  • have you tried that using the array method – Gideon Appoh Jul 01 '15 at 04:22
  • use `INSERT INTO test_table1 (`item1`, `item2`, `item3`)` ``SELECT 'value of item1', 'value of item2', `value_of_item3` FROM ...`` – Ryan Vincent Jul 01 '15 at 05:06
  • @GideonAppoh in array method this is exactly the same it. this is a simple example of my operations. – Morteza Jul 01 '15 at 12:39
  • @RyanVincent can you explain a little more? – Morteza Jul 01 '15 at 12:41
  • Useful? [MySQL INSERT INTO … VALUES and SELECT](https://stackoverflow.com/questions/15523597/mysql-insert-into-values-and-select). You either use the `VALUES` keyword and list the values, or, you use a `SELECT ...` to get the values to insert. You cannot mix the two methods. Therefore you need to add the 'constant' values to the `select` query. It will work exactly as you expect. – Ryan Vincent Jul 01 '15 at 13:14
  • @RyanVincent run my first code. it work very well, then we can mixed it on Mysql but we cant mixed on PDO. you have any idea for this in PDO? – Morteza Jul 08 '15 at 19:37

0 Answers0