Re: Doubts concerning a general Insert method

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

 



2009/7/13 MEM <talofo@xxxxxxxxx>:
> Hello, I'm trying to understand a general CRUD class that I've seen here:
> http://www.phpro.org/classes/PDO-CRUD.html
>
> I'm learning PHP and I have some doubts on this method to generally insert
> data into DB. The class name is crud and here is the method:
>
> public function dbInsert($table, $values) {
>
>        $this->conn();
>
>        $fieldnames = array_keys($values[0]);
>
>        $size = sizeof($fieldnames);
>
>        $i=1;
>
>        //construction of the prepared statment
>        $sql = "INSERT INTO $table";
>
>        $fields = '( ' . implode(' ,', $fieldnames) . ' )';
>
>        $bound = '(:' . implode(', :', $fieldnames) . ' )';
>
>        $sql .= $fields.' VALUES '.$bound;
>
>        //prepares statement e saves it on variable $stmt
>        $stmt = $this->db->prepare($sql);
>
>        foreach($values as vals)
>        {
>                $stmt->execute($vals);
>        }
> }
>
>
> To place values on the DB we do:
>
> $crud = new crud();
>
> $values = array
>            (
>                array('animal_name'=>'bruce', 'animal_type'=>'dingo'),
>                array('animal_name'=>'bruce', 'animal_type'=>'kangaroo'),
>            );
>
> $crud->dbInsert('animals', $values);
>
>
>
>
>
> The doubts:
> 1) Names convention question:
> Isn't more correct to call $columname, instead of $fieldname ?

The two terms are interchangeable in the context of a database.

> 2) Why do we have this?
>  $i=1

It's not used so I'd guess it's a remnant from an older version of the
method. Safe to remove it.

> 3) Here:
> $fieldnames = array_keys($values[0]);
>
> We are keeping on variable $fieldnames, the key value of the $values array,
> when this array is on the position 0 ? And what is *actually* the value
> returned, considering our array?
>
> $values = array
>            (
>                array('animal_name'=>'bruce', 'animal_type'=>'dingo'),
>                array('animal_name'=>'bruce', 'animal_type'=>'kangaroo'),
>            );

You can use the var_dump function to dump the contents of $fieldnames
after that line has been executed and see for yourself.

In this case let's break it down...

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

> 4) Here:
> foreach($values as $vals)
>        {
>                $stmt->execute($vals);
>        }
>
> We are telling that, for each (line/element/index ???) of $values array, the
> actual value will be "given"(?) to vals, and the pointer goes to the next
> (line/element/index)... ?
>
> We then execute the prepared statement, but I don't get what are we passing
> as a param? I mean, what kind of think does the execute PDO method expects
> as a param?
> Why $stmt->execute($vals); and not only $stmt->execute(); ?

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.

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.

For more info I suggest you Google for "PDO prepared statements" for
further reading.

-Stuart

-- 
http://stut.net/

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