RE: Doubts concerning a general Insert method

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>     $values[0] will give you the first element of $values, namely
> array('animal_name'=>'bruce', 'animal_type'=>'dingo').
> 
>     array_keys will return an array containing the keys from the
> passed array, so in this case you'll get array('animal_name',
> 'animal_type').
>

So... since $value is an associate array of arrays, we will get, on the first key, not an array with "0, 1", like array(0,1); but array('animal_name','animal_type'), yes?
When we use the implode over this array, we get:
animal_name, animal_type that is the string that will pass to be prepare using the PDO prepare().
 
 
> After it's finished building $sql use var_dump to look at it. You'll
> see that the values are specified as :animal_name and :animal_type.
> The : indicates to PDO that these are replaceable values.

Yes. And normally, to fill those replaceable values, I was used to use bindParam();
I like this bindParam method because we can then use PDO::PARAM_INT and PDO::PARAM_STR to more accurately control the data type flow...

> 
> The foreach will go through the $values array and for each row it will
> pass the data (e.g. array('animal_name'=>'bruce',
> 'animal_type'=>'dingo') for the first time round the loop) to the
> execute function which will effectively replace those elements in the
> SQL statement and execute it.

Ok, so:
Our $sql will be: INSERT INTO $table (animal_name, animal_type) VALUES (:animal_name, :animal_type) 

We then prepare this $sql by doing: 
prepare($sql); and the value of this preparation will be kept on a variable name $stmt.

Finally, on the foreach, we will grab each value of the $values array, and keep him, on a variable called $vals, 

The $vals will contain this on the first occurrence of the loop:
array('animal_name'=>'bruce', 'animal_type'=>'ding')

and then, the var $vals will have this on the second occurrence of the loop:
array('animal_name'=>'bruce', 'animal_type'=>'kanguro')

etc.,

At the end of each of these loops, we will process the execute (that will send the statement to the database).
$stmt->execute(array('animal_name'=>'bruce', 'animal_type'=>'kanguro').

So this execute will do A LOT, it will take away the 'array(' part, will see the keys of these arrays (e.g. animal_name and animal_type) compare them with the placeholder names given on the prepare statement and, replace the placeholder names with the values inside on each of this array keys.


Is this correct?



Regards,
Márcio




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux