0

How would i write a function to accept X number of parameters

for ex if i want to write a function to insert into a db. i dont know how many fields i will be inserting into the table?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by kireol
0

just use an array to do this.


example.

$data = array ('name'=>'john doe', 'department' => 'sales', 'age'=> 39);
insert_record($data);

function insert_record($data)
{
   $sql = 'insert into companytable ';
   foreach($data as $field=>$value)
  {
      //build sql here
      $sql .= do stuff here
  }
  //execute sql command
}

Edited by kireol: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.