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

Kevin
  • 2,684
  • 6
  • 35
  • 64

2 Answers2

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.

Community
  • 1
  • 1
Brock Hensley
  • 3,617
  • 2
  • 29
  • 47
  • it is needed the way I have it... Value => Datatype – Kevin Mar 05 '13 at 14:20
  • 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 – Brock Hensley Mar 05 '13 at 14:33
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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • `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? – Kevin Mar 05 '13 at 14:19
  • 2
    The **only** case known to me is listed in the [pdo tag wiki](http://stackoverflow.com/tags/pdo/info) under `"PDO prepared statements and LIMIT"` title. – Your Common Sense Mar 05 '13 at 14:22