Re: [PHP] combine implode() and array_keys() to get a string of key names

2006-07-13 Thread Richard Lynch
On Mon, July 10, 2006 2:14 am, Larry Garfield wrote:
 It is slightly more complicated than that, since if the value is
 numeric and
 going into a numeric field, then it's not supposed to be quoted.
 (MySQL
 generally doesn't care, but some other databases may; I'm not
 certain.)

AFAIK, MySQL is the ONLY SQL database to let you get away with putting
such bogosity as adding apostrophes and/or quotes around integer and
float values.

All the rest are gonna bitch up a storm if you do that.

:-)

-- 
Like Music?
http://l-i-e.com/artists.htm

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



Re: [PHP] combine implode() and array_keys() to get a string of key names

2006-07-10 Thread Larry Garfield
On Friday 07 July 2006 10:03, Janet Valade wrote:

 Actually, it doesn't matter what the indexes are when you use implode.
 Implode just puts the values into a string. Here's code that will work.

$field_array = array_keys($fields_form);
$fields = implode(,,$field_array);
$values = implode(',',$fields_form);
$query = INSERT INTO Table1 ($fields) VALUES (\$values\);
$result = mysqli_query($cxn,$query);

 $fields_form is an associative array of all the fields to be entered
 into the database, with the field names as keys and the field values as
 values. This code quotes the values in the $values string in the query,
 as needed if the values are strings.

It is slightly more complicated than that, since if the value is numeric and 
going into a numeric field, then it's not supposed to be quoted.  (MySQL 
generally doesn't care, but some other databases may; I'm not certain.)  

I'm actually currently in the midst of getting this functionality added to the 
Drupal CMS.  Feel free to look at the patch I've submitted[1].  It has some 
Drupal-isms in it, but for the most part is fairly clear.  It also handles 
insert, update, and delete, too.

[1] http://drupal.org/node/30334

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

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



[PHP] combine implode() and array_keys() to get a string of key names

2006-07-07 Thread Dave M G

PHP List,

   I've got a series of associative arrays that contain simple string 
values that I want to insert into my database.


   In each array, the names of the keys correspond to the column names 
in the database table. The values stored in the array are, of course, 
the values that I want to insert into the columns.


   So, I want to set up a function where I can pass the array as an 
argument, and it will construct the MySQL statement using the keys for 
column names, and array values for insert  values.


   Within the function, the query looks like this:
   $query = INSERT INTO table ( . $columns . ) VALUES = ( . $values 
. );


   Assuming that the array argument passed to the function is called 
$array, converting the array values to the insert values is pretty easy:

   $values = implode(, , $array);

   But in the case of using the array keys for column names, it's not 
so easy:

   $columns = implode(,  array-keys($array));

   I know that code above doesn't work, because, as described in the 
manual, array keys returns a list of the numerical indexes along with 
the string indexes. Something like this:


Array
(
   [0] = column1
   [1] = column2
   [2] = column3
)

   At least, that's what it looks like if it's just echoed out.

   Is there a way I can strip out the relevant column names to be more 
like this:


column1, column2. column3

   Thank you for any advice.

--
Dave M G

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



Re: [PHP] combine implode() and array_keys() to get a string of key names

2006-07-07 Thread Jochem Maas
Dave M G wrote:
 PHP List,
 
I've got a series of associative arrays that contain simple string
 values that I want to insert into my database.
 
In each array, the names of the keys correspond to the column names
 in the database table. The values stored in the array are, of course,
 the values that I want to insert into the columns.
 
So, I want to set up a function where I can pass the array as an
 argument, and it will construct the MySQL statement using the keys for
 column names, and array values for insert  values.
 
Within the function, the query looks like this:
$query = INSERT INTO table ( . $columns . ) VALUES = ( . $values
 . );
 
Assuming that the array argument passed to the function is called
 $array, converting the array values to the insert values is pretty easy:
$values = implode(, , $array);
 
But in the case of using the array keys for column names, it's not so
 easy:
$columns = implode(,  array-keys($array));
 
I know that code above doesn't work, because, as described in the
 manual, array keys returns a list of the numerical indexes along with
 the string indexes. Something like this:
 
 Array
 (
[0] = column1
[1] = column2
[2] = column3
 )
 
At least, that's what it looks like if it's just echoed out.
 
Is there a way I can strip out the relevant column names to be more
 like this:
 
 column1, column2. column3
 
Thank you for any advice.

I'll assume you 'data' array only contains associative keys for simpilicity,
here we go:

function davesMakeValueSafeForDBInsertionFunc($v)
{
if (is_null($arg)) return 'NULL';
if (is_numeric($arg)) return $arg;

return '.mysql_real_escape_string($arg).';
}

function davesFunkyDBElementQuoterFunc($elName)
{
return `{$elName}`;
}

$data  = array(
'col1' = 1,
'col2' = null,
'col3' = yadda yadda,
);

$columns = join(',', array_map('davesFunkyDBElementQuoterFunc', 
array_keys($data)));
$values  = join(',', array_map('davesMakeValueSafeForDBInsertionFunc', $data));
$query   = INSERT INTO table ($columns) VALUES ($values);

// tada!

 
 -- 
 Dave M G
 

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



Re: [PHP] combine implode() and array_keys() to get a string of key names

2006-07-07 Thread Dimiter Ivanov

On 7/7/06, Dave M G [EMAIL PROTECTED] wrote:

PHP List,

I've got a series of associative arrays that contain simple string
values that I want to insert into my database.

In each array, the names of the keys correspond to the column names
in the database table. The values stored in the array are, of course,
the values that I want to insert into the columns.

So, I want to set up a function where I can pass the array as an
argument, and it will construct the MySQL statement using the keys for
column names, and array values for insert  values.

Within the function, the query looks like this:
$query = INSERT INTO table ( . $columns . ) VALUES = ( . $values
. );

Assuming that the array argument passed to the function is called
$array, converting the array values to the insert values is pretty easy:
$values = implode(, , $array);

But in the case of using the array keys for column names, it's not
so easy:
$columns = implode(,  array-keys($array));

I know that code above doesn't work, because, as described in the
manual, array keys returns a list of the numerical indexes along with
the string indexes. Something like this:

Array
(
[0] = column1
[1] = column2
[2] = column3
)

At least, that's what it looks like if it's just echoed out.

Is there a way I can strip out the relevant column names to be more
like this:

column1, column2. column3

Thank you for any advice.

--
Dave M G

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



Well after calling array_keys you have an array with all the keys.
If you implode it the same way like you get the values from the
original array, you will get what you want.
$values = implode(, , $array);
$keys = array_keys($array);
$columns = implode(, ,$keys);

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



Re: [PHP] combine implode() and array_keys() to get a string of key names

2006-07-07 Thread Janet Valade

Dave M G wrote:


PHP List,

   I've got a series of associative arrays that contain simple string 
values that I want to insert into my database.


   In each array, the names of the keys correspond to the column names 
in the database table. The values stored in the array are, of course, 
the values that I want to insert into the columns.


   So, I want to set up a function where I can pass the array as an 
argument, and it will construct the MySQL statement using the keys for 
column names, and array values for insert  values.


   Within the function, the query looks like this:
   $query = INSERT INTO table ( . $columns . ) VALUES = ( . $values 
. );


   Assuming that the array argument passed to the function is called 
$array, converting the array values to the insert values is pretty easy:

   $values = implode(, , $array);

   But in the case of using the array keys for column names, it's not so 
easy:

   $columns = implode(,  array-keys($array));

   I know that code above doesn't work, because, as described in the 
manual, array keys returns a list of the numerical indexes along with 
the string indexes. 


Actually, it doesn't matter what the indexes are when you use implode. 
Implode just puts the values into a string. Here's code that will work.


  $field_array = array_keys($fields_form);
  $fields = implode(,,$field_array);
  $values = implode(',',$fields_form);
  $query = INSERT INTO Table1 ($fields) VALUES (\$values\);
  $result = mysqli_query($cxn,$query);

$fields_form is an associative array of all the fields to be entered 
into the database, with the field names as keys and the field values as 
values. This code quotes the values in the $values string in the query, 
as needed if the values are strings.


Janet



Something like this:


Array
(
   [0] = column1
   [1] = column2
   [2] = column3
)

   At least, that's what it looks like if it's just echoed out.

   Is there a way I can strip out the relevant column names to be more 
like this:


column1, column2. column3

   Thank you for any advice.

--
Dave M G




--
Janet Valade -- janet.valade.com

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