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

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

am 07.01.2008 05:15:21 von Rowan

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.


sql 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
( ?, ?, ?, ?, ?, ?, ?, ?, ?);



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
';
}
print "$sql
";
$stmnt = db_cxn::$dbh->prepare($sql);
foreach ($data as $arr) {
//krumo($arr);
$i = 1;
foreach ($arr as $key =>$value){
print "$value
";
$stmnt->bindParam($i, $value);
$i++;
}
$stmnt->execute();

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

}

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

am 07.01.2008 19:38:27 von giblfiz

> $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.

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

am 07.01.2008 23:25:22 von Rowan

On Jan 7, 10:38 am, giblfiz wrote:
> > $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.

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

am 07.01.2008 23:31:20 von Rowan

On Jan 6, 8:15 pm, Rowan wrote:
> 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.