Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO bindParam into one statement?

Tags:

php

mysql

pdo

Is there a way I can put these bindParam statements into one statement?

$q = $dbc -> prepare("INSERT INTO accounts (username, email, password) VALUES (:username, :email, :password)");
$q -> bindParam(':username', $_POST['username']);
$q -> bindParam(':email', $_POST['email']);
$q -> bindParam(':password', $_POST['password']);
$q -> execute();

I was using mysqli prepared before where it was possible, I switched to PDO for assoc_array support. On the php.net website for PDO it shows them on seperate lines, and in all examples I have seen it is on seperate lines.

Is it possible?

like image 261
Basic Avatar asked Apr 16 '11 02:04

Basic


1 Answers

Example 2 on the execute page is what you want:

$sth->execute(array(':calories' => $calories, ':colour' => $colour));

You may want to look at the other examples too. With question mark parameters, it would be:

$q = $dbc -> prepare("INSERT INTO accounts (username, email, password) VALUES (?, ?, ?)");
$q->execute(array($_POST['username'], $_POST['email'], $_POST['password']));

If those are the only columns, you can just write:

$q = $dbc -> prepare("INSERT INTO accounts VALUES (?, ?, ?)");
$q->execute(array($_POST['username'], $_POST['email'], $_POST['password']));
like image 141
Matthew Flaschen Avatar answered Sep 28 '22 16:09

Matthew Flaschen