By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

dumping a array into PDO::db using place holders.

P: n/a

I'm trying to enter data from an array ( eg entry1=(x=>y,a=b),
entry2=(x=>y,a=>c)) I'm trying to use place holders to dump the data.
It maybe late but I can't get this to work. below is the actual
routine used to exec. any idea on how to better format this.
<snippetsql statement
INSERT INTO contacts ( name_first, name_last, personal_street,
personal_city, personal_state, personal_zip, personal_phone_home,
company_phone, personal_phone_cell) VALUES
( ?, ?, ?, ?, ?, ?, ?, ?, ?);
</snippet>

<snippet>
public function db_update_i($sql,$data) {
$str_check = explode(" ", $sql);
if($str_check[0] == "INSERT") {
if (is_null(db_cxn::$dbh)){
db_cxn::$dbh = self::cxn();
print 'db_cxn::db_update::a new instance of $dbh <br/>';
}
print "$sql <br/>";
$stmnt = db_cxn::$dbh->prepare($sql);
foreach ($data as $arr) {
//krumo($arr);
$i = 1;
foreach ($arr as $key =>$value){
print "$value <br/>";
$stmnt->bindParam($i, $value);
$i++;
}
$stmnt->execute();

}
$stmnt = null;
$sql = null;
return 1;
}else{
print "Error!: Incorrectly Formatted SQL Statement";
}
}

}
</snippet>
Jan 7 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
$stmnt->bindParam($i, $value);

I'm not 100% about this, but doesn't binddParam want a data type
specified, something like:
$stmnt->bindParam($i, $value, PDO::PARAM_STR, strlen($value));

Not my usual DB library, so this is just a shot in the dark.
Jan 7 '08 #2

P: n/a
On Jan 7, 10:38 am, giblfiz <gibl...@gmail.comwrote:
$stmnt->bindParam($i, $value);

I'm not 100% about this, but doesn't binddParam want a data type
specified, something like:
$stmnt->bindParam($i, $value, PDO::PARAM_STR, strlen($value));
tried that, no luck. I think the problem is bindParam expects a
variable name. however I would like to just loop through the array.
This way I can use the same function to input arrays.
Jan 7 '08 #3

P: n/a
On Jan 6, 8:15 pm, Rowan <yaard...@gmail.comwrote:
I'm trying to enter data from an array ( eg entry1=(x=>y,a=b),
entry2=(x=>y,a=>c)) I'm trying to use place holders to dump the data.
It maybe late but I can't get this to work. below is the actual
routine used to exec. any idea on how to better format this.
figured out the problem. For reference see below I got this from the
php.net page.

If you're using bindParam in a loop such as this:

$counter=1;
foreach($email as $val){
$stmt->bindParam($counter, $val, PDO::PARAM_STR);
$counter++;
}

It will fail because $val is local and the variable is bound as a
reference and will only be evaluated at the time that PDOStatement-
>execute() is called.
So use bindValue instead.
Jan 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.