1

I am attempting to properly bind parameters to a statement with the Function below. I am passing in an associative array, value=>datatype.

I am getting an error though while attempting this: Notice: Undefined offset: 0 in db.class.php on line 69 Notice: Undefined offset: 1 in db.class.php on line 69

Line 69 is the for line in Function

How do I fix this? And/Or should I even bother doing it like this?

Here's Function

protected function ConvertParams($stmt, $params){ $parrs = $params; if(is_array($parrs)){ $parrCt = count($parrs); echo '<pre>'; print_r($parrs); echo '</pre>'; echo '<hr />'; for($i = 0; $i < $parrCt; ++$i){ switch ($parrs[$i][1]){ case 'string': $stmt->bindParam($i + 1, $parrs[$i][0], PDO::PARAM_STR); break; case 'int': $stmt->bindParam($i + 1, $parrs[$i][0], PDO::PARAM_INT); break; case 'boolean': $stmt->bindParam($i + 1, $parrs[$i][0], PDO::PARAM_BOOL); break; case 'lob': $stmt->bindParam($i + 1, $parrs[$i][0], PDO::PARAM_LOB); break; default: $stmt->bindParam($i + 1, $parrs[$i][0]); } } } } 

Here's the array:

$db->Params = array('%a%'=>'string', '%Welcome%'=>'string'); 

Please assume that I am connecting to the db correctly, and that the query properly executes (minus this bindParam issue).

1
  • Can you please post the sample code where this function is used? Commented Mar 5, 2013 at 13:57

2 Answers 2

0

You don't need neither "proper way of binding" nor whole this function at all. Just pass an array of parameters straight to execute:

$params = array('%a%', '%Welcome%'); $stmt->execute($params); 

it will dramatically shorten your code without any drawbacks.

Please keep your code clean and simple.
Useless complication will make it hard to support and maintain.

Sign up to request clarification or add additional context in comments.

2 Comments

Useless complication will make it hard to support and maintain. =) I agree, I come from a Classic ASP background, so I assumed parameterizing would be the proper way to do it. Is there any setbacks to doing how you posted?
The only case known to me is listed in the pdo tag wiki under "PDO prepared statements and LIMIT" title.
0

My first post... I could be wrong here, but I believe there is no [0] or [1] index of $parrs as you're passing it with named index of '%a%' and '%Welcome%'.

You could try replacing the [0] and [1] with %a% and %Welcome% respectively, or using numeric indexes for the array.

2 Comments

it is needed the way I have it... Value => Datatype
I'm not saying you have to change your array. But you can't reference [0] if the array doesn't have numerical indexes. Call it by its named index, or get the numerical indexes per my links. Thanks for reading and giving me a -1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.