[PHP] Query result column to array

2006-08-30 Thread Christopher Watson

I'm looking for a native method for taking all of the values from one
column of a query result and creating an array of those values.
Procedurally, I'd tend to want to do this:

?php
$my_id_array = array();
$query_result = $_DB-query(SELECT my_id FROM my_table);
while ($row = $query_result-fetchRow())
   $my_id_array[] = $row['my_id'];
?

Heck, ColdFusion has the ValueList function which does exactly what I
want (of course, it returns a delimited list [string], not a native
array, but the one-step method is there).  Is there anything in
PHP-land that does that?  Or am I pretty much stuck using some
variation of the code above?

Christopher Watson
Principal Architect
The International Variable Star Index (VSX)
http://vsx.aavso.org

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



Re: [PHP] Query result column to array

2006-08-30 Thread Rafael Mora

Hi!!

What I do is just this:

$_ar = array();


  while($_field = mysql_fetch_row($_result))
  {$_aloft[$_field[0]] = $_field[1];}

  while(list($_key,$_val) = each($_ar))


On 8/30/06, Christopher Watson [EMAIL PROTECTED] wrote:


I'm looking for a native method for taking all of the values from one
column of a query result and creating an array of those values.
Procedurally, I'd tend to want to do this:

?php
$my_id_array = array();
$query_result = $_DB-query(SELECT my_id FROM my_table);
while ($row = $query_result-fetchRow())
   $my_id_array[] = $row['my_id'];
?

Heck, ColdFusion has the ValueList function which does exactly what I
want (of course, it returns a delimited list [string], not a native
array, but the one-step method is there).  Is there anything in
PHP-land that does that?  Or am I pretty much stuck using some
variation of the code above?

Christopher Watson
Principal Architect
The International Variable Star Index (VSX)
http://vsx.aavso.org

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




Re: [PHP] Query result to an array

2002-09-12 Thread Christian Ista


   $result = mysql_query(...);
   while( $t = mysql_fetch_row($result) ){
 $array[] = $t;
   }

No. I need a 2 dimensions arrays, each line of this array, is the result 
of one row of the query and each cell of this row is a field from the 
select.

Example, the query return 2 row and 3 fields for each row :
Christian, Ista, Belgium
zak, PhpNet, Dontknowfrom

resulf of SELECT NAME, LASTNAME, COUNTRY FROM MyTable;

I'd like to put that in an array and later because I use a session array.

I'd like via a for loop access these data by
echo(MyArray[1][1]);  result is PhpNet
echo(MyArray[0][2]);  result is Belgium
echo(MyArray[1][0]);  result is zak

Is it clearer now ?

Thanks,

Christian



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




RE: [PHP] Query result to an array

2002-09-12 Thread Martin Towell

$result = mysql_query(...);
   while( $t = mysql_fetch_row($result) ){
 $array[] = $t;
   }
 
 No. I need a 2 dimensions arrays, each line of this array, is the result 
 of one row of the query and each cell of this row is a field from the 
 select.
 
 Example, the query return 2 row and 3 fields for each row :
 Christian, Ista, Belgium
 zak, PhpNet, Dontknowfrom
 
 resulf of SELECT NAME, LASTNAME, COUNTRY FROM MyTable;
 
 I'd like to put that in an array and later because I use a session array.
 
 I'd like via a for loop access these data by
 echo(MyArray[1][1]);  result is PhpNet
 echo(MyArray[0][2]);  result is Belgium
 echo(MyArray[1][0]);  result is zak
 
 Is it clearer now ?
 
 Thanks,
 
 Christian
 

That's exactly what this will do. mysql_fetch_row() returns an array.
Appending that to another array and you get back an array of arrays (which
is the same as a 2D array)

Try it and see...

Martin


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




Re: [PHP] Query result to an array

2002-09-12 Thread Zak Greant

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On September 12, 2002 01:55, Christian Ista wrote:
$result = mysql_query(...);
while( $t = mysql_fetch_row($result) ){
  $array[] = $t;
}

 No. I need a 2 dimensions arrays, each line of this array, is the result
 of one row of the query and each cell of this row is a field from the
 select.

  Did you test the code? :)

 Example, the query return 2 row and 3 fields for each row :
 Christian, Ista, Belgium
 zak, PhpNet, Dontknowfrom

 resulf of SELECT NAME, LASTNAME, COUNTRY FROM MyTable;

  The code snippet above combined with the above query would create an array
  of this structure:

  $array[0][0] = 'Christian';
  $array[0][1] = 'Ista';
  $array[0][2] = 'Belgium';

  $array[1][0] = 'zak';
  $array[1][1] = 'PhpNet';
  $array[1][2] = 'Canada';

 I'd like to put that in an array and later because I use a session array.

 I'd like via a for loop access these data by
 echo(MyArray[1][1]);  result is PhpNet
 echo(MyArray[0][2]);  result is Belgium
 echo(MyArray[1][0]);  result is zak

 Is it clearer now ?

Cheers!
- --zak  
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9gD/1b6QONwsK8bIRAmm7AJ41PUo2zVmFwN/vfvTr4j6Fze/NdwCbBPZm
bWnEPyBj+bKQsnQ2bg9HZ7E=
=q8vy
-END PGP SIGNATURE-


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




[PHP] Query result to an array

2002-09-11 Thread Christian Ista

Hello,

A query return x rows, by rows there are 4 fields. I'd like to put the
result in an array, a 2 dimensions array. Could you tell me how to do
that ?

Bye



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




Re: [PHP] Query result to an array

2002-09-11 Thread Zak Greant

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On September 11, 2002 14:12, Christian Ista wrote:
 Hello,

 A query return x rows, by rows there are 4 fields. I'd like to put the
 result in an array, a 2 dimensions array. Could you tell me how to do
 that ?

  Something like this may work for you:

# Connect to db, make query, etc.

while( $temp = mysql_fetch_row($mysql_result_handle) ) {
$array[] = array(
'key0' = array(
'keya' = $temp[0],
'keyb' = $temp[1],
),
'key1' = array(
'keya' = $temp[2],
'keyb' = $temp[3],
)
);
}

Good luck!
- --zak
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9f6tAb6QONwsK8bIRAvjKAJwKN0yvbGaztQzZOKYiy22FMqQ5EgCeP3AH
4sGpVTTX6gUJp8fZJ8xXEfQ=
=eaeg
-END PGP SIGNATURE-


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




RE: [PHP] Query result to an array

2002-09-11 Thread Christian Ista

 while( $temp = mysql_fetch_row($mysql_result_handle) ) {
   $array[] = array(
   'key0' = array(
   'keya' = $temp[0],
   'keyb' = $temp[1],
   ),
   'key1' = array(
   'keya' = $temp[2],
   'keyb' = $temp[3],
   )
   );
 }

Hello,

Thanks for your help. But that's not work at all.

bye



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




Re: [PHP] Query result to an array

2002-09-11 Thread Zak Greant

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On September 11, 2002 15:05, Christian Ista wrote:
  while( $temp = mysql_fetch_row($mysql_result_handle) ) {
  $array[] = array(
  'key0' = array(
  'keya' = $temp[0],
  'keyb' = $temp[1],
  ),
  'key1' = array(
  'keya' = $temp[2],
  'keyb' = $temp[3],
  )
  );
  }

 Hello,

 Thanks for your help. But that's not work at all.

  Dear Christian,

  I assume that you encountered some syntax errors in the code?
  (or was there another problem?)

  Anyhow, here is the corrected version

while( $temp = mysql_fetch_row($mysql_result_handle) ) {
$array[] = array(
'key0' = array(
'keya' = $temp[0],
'keyb' = $temp[1]
),
'key1' = array(
'keya' = $temp[2],
'keyb' = $temp[3]
)
);
}

- --zak
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9f7K3b6QONwsK8bIRAiATAJ9TPv/28hZiF136TePF5SXTgDinUACgkg6w
BJVDvRVsVzqplDLOD/u5u4E=
=NbgV
-END PGP SIGNATURE-


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




Re: [PHP] Query result to an array

2002-09-11 Thread Zak Greant

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On September 11, 2002 15:21, Christian Ista wrote:
  while( $temp = mysql_fetch_row($mysql_result_handle) ) {
  $array[] = array(
  'key0' = array(
  'keya' = $temp[0],
  'keyb' = $temp[1]
  ),
  'key1' = array(
  'keya' = $temp[2],
  'keyb' = $temp[3]
  )
  );
  }

 I corrected this error myself but that's not work.

 Why that twice ? (only the key change)

 'key0' = array(
   'keya' = $temp[0],
   'keyb' = $temp[1]
   ),

 when I do echo($array[0][1]) I receive array unknown.

  Ok - if you want a numeric array, then do something like this:

  array(
array( $temp[0], $temp[1] ),
array( $temp[1], $temp[2] )
  )

  --zak
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9f7p3b6QONwsK8bIRAhEpAJ4vU+f/AovqfUkHvdnBf6le47N9AACfdK7K
L6Ub877d4ZUwbxSXF6gl9qY=
=Vnxt
-END PGP SIGNATURE-


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




Re: [PHP] Query result to an array

2002-09-11 Thread Christian Ista

   array(
   array( $temp[0], $temp[1] ),
   array( $temp[1], $temp[2] )
   )

I thinks it's not the right way.

I have a query, this query can return 1,5,20, 200, ... rows, each row has 
5 fields (or more or less).

I'd like a 2 dimensions array, one line by row and each cell is a field 
content of the query.

I program in a lot of language C/C++, Java, Delphi, C++Builder, 
ColdFusion, C# and I don't undersand why the PHP syntax for this kind of 
think is so complex and not clear.

Bye


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




RE: [PHP] Query result to an array

2002-09-11 Thread Martin Towell

I'm not totally familiar with mySql, but if I remember correctly, you can
get each row (one at a time) back as an (1D) array. All you need to do is
append that array onto the end of your dataset array and, bob's your uncle,
you have a 2D array full with your dataset

HTH
Martin

-Original Message-
From: Christian Ista [mailto:[EMAIL PROTECTED]]
Sent: Thursday, September 12, 2002 4:56 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Query result to an array


   array(
   array( $temp[0], $temp[1] ),
   array( $temp[1], $temp[2] )
   )

I thinks it's not the right way.

I have a query, this query can return 1,5,20, 200, ... rows, each row has 
5 fields (or more or less).

I'd like a 2 dimensions array, one line by row and each cell is a field 
content of the query.

I program in a lot of language C/C++, Java, Delphi, C++Builder, 
ColdFusion, C# and I don't undersand why the PHP syntax for this kind of 
think is so complex and not clear.

Bye


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

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




Re: [PHP] Query result to an array

2002-09-11 Thread OrangeHairedBoy

Here's a function I wrote...feel free to use it. It will convert anything
into a 2D array.



Sample Table:

ID  Name

1Bob

2Joe

3Eric

4Cody



$query=select * from sampletable;

$result=mysql_query($query);

$wholetable=ResultToArray($result,ID);

foreach ($wholetable as $key=$value)

{

print $key. $value[Name];

}



function ResultToArray ( $result , $key )

{

while ( $row = mysql_fetch_object ( $result ) )

{

foreach ( $row as $column = $data )

{

$thiskey=$row-$key;

$table[$thiskey][$column] = $data;

}

}

}



I hope this makes sense!!



Lewis





Christian Ista [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
array(
  array( $temp[0], $temp[1] ),
  array( $temp[1], $temp[2] )
)

 I thinks it's not the right way.

 I have a query, this query can return 1,5,20, 200, ... rows, each row has
 5 fields (or more or less).

 I'd like a 2 dimensions array, one line by row and each cell is a field
 content of the query.

 I program in a lot of language C/C++, Java, Delphi, C++Builder,
 ColdFusion, C# and I don't undersand why the PHP syntax for this kind of
 think is so complex and not clear.

 Bye




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




Re: [PHP] Query result to an array

2002-09-11 Thread Zak Greant

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On September 12, 2002 00:56, Christian Ista wrote:
array(
  array( $temp[0], $temp[1] ),
  array( $temp[1], $temp[2] )
)

 I thinks it's not the right way.

 I have a query, this query can return 1,5,20, 200, ... rows, each row has
 5 fields (or more or less).

 I'd like a 2 dimensions array, one line by row and each cell is a field
 content of the query.

 I program in a lot of language C/C++, Java, Delphi, C++Builder,
 ColdFusion, C# and I don't undersand why the PHP syntax for this kind of
 think is so complex and not clear.

  Good Lord! I thought that you wanted to transform an of array query results
  into a multi-dimensional array.

  Briefly, you want to do this:

  $result = mysql_query(...);
  while( $t = mysql_fetch_row($result) ){
$array[] = $t;
  }

  Just read the MySQL section in the PHP manual for more details.

Cheers!
- --zak
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9gDCIb6QONwsK8bIRAitjAJ0bUVpwvcdbrIayNQFtbfPqtHJSkwCdGmrq
OIkNSi0vING+U/+/GgYf/58=
=d63f
-END PGP SIGNATURE-


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