Re: [phplib] Canīt replace the fields using number off filed From: Mike Green (Mike.Green <email protected>)
Date: 06/22/01

Julio,

Here is one way (if I understand correctly what it is you want to do):

$col_names = array ("firstname", "lastname", "city");
$firstname = "Julio";
$lastname = "Oliveira";
$city = "Buenos Aires";
reset($col_names); // not necessary here, but a good safety measure
$sql = "INSERT INTO personal_info_table (";
$sql_end = ") VALUES (";
while (list($key, $val) = each($col_names)){
//see http://www.php.net/manual/es/function.each.php
     if ($key) {
         $sql .= ", ";
         $sql_end .= ", ";
     }
     $sql .= $val;
     $sql_end .= "'".${$val}."'";
     //see http://www.php.net/manual/es/language.variables.variable.php
}
$sql .= $sql_end.")";
// Get a database connection
$db = new DB_your_db_class;
// extended from the PHPLib Class DB_Sql:
http://phplib.netuse.de/documentation/documentation-3.html#ss3.1
$db->query($sql);

And here is another, slightly different, way:

$col_names = array ("firstname" => "Julio",
                               "lastname" => "Oliveira",
                               "city" => "Buenos Aires");
reset($col_names);
$sql = "INSERT INTO personal_info_table (";
$sql_end = ") VALUES (";
$col_nr = 0;
while (list($key, $val) = each($col_names)) {
     if ($col_nr++) {
         $sql .= ", ";
         $sql_end .= ", ";
     }
     $sql .= $key;
     $sql_end .= "'$val'";
}
$sql .= $sql_end.")";
$db = new DB_your_db_class;
$db->query($sql);

Cheers!

Mike Green

Julio wrote:

> hi
> Sorry by my English.
> Iīm new to php i have a lot off comercial soft at dos in Clipper.
> I need to replace some fields base in at the number off field and not the name to do a while
>
> _var[1] = "julio"
> _var[2] = "Oliveira"
> _var[3] = "Buenos Aires"
> for x = 1 to 3
> xgrabo(x,_var[x]) // mysql replace or insert
> next
>
> something like this in php lenguaje.
>
> Thanks in advanced
> Julio Oliveira - Buenos aires - Argentina

--
Mike Green
SaeSolved::
http://www.saesolved.com
http://www.everypeople.net
http://www.sitewidgets.com
http://www.widgetchuck.com

--------------------------------------------------------------------- To unsubscribe, e-mail: phplib-unsubscribe <email protected> For additional commands, e-mail: phplib-help <email protected>