Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: How to save SQL query results into an array?


Message #1 by guru@i... on Tue, 13 Aug 2002 16:39:20
> 
> Thats the first decent, logical solution I have seen in 4 days.

Thanks for the compliment... all I can say is "You should've asked sooner!"

=)


> Say I wanted to print everything in the array after populating it
> with the data from the search result, how do I go about it?
>
> Say I wanted to access the 8th item in the array, should i be doing ECHO
> $row[8].['MYNAME'] ??
>
> Is that correct?

It's close... you don't need the '.', though.  '.' is the string
concatenation operator, which expects a string on either side of it.

For instance,  "Hello" . ", world"  is an expression which evaluates to the
string "Hello, world".

$row[8].['MYNAME'] is an expression, but PHP will not know how to deal with
it.  Let's step through it to find out why:

Each side of the expression should evaluate to a string.

The left side is $row[8], which is an array.  PHP handles this conversion
simply by converting the original array to the string "Array".

The right hand side is ['MYNAME'], which PHP doesn't know what to do with -
-
you'll likely get a parse error about an unexpected left bracket or
something.


To print the 8th name in your result data, you simply do this:

  echo $rows[7]['MYNAME'] would do it.

(I use 7 because array indexes begin at 0.)


If it helps to visualize multidimensional arrays, you can take things one
part at a time.


$rows is an array, with numerical indexes, where the value at each index a
row of result data from an SQL query.

$rows[$i] is an array, with string indexes MYNAME and MYAGE, which
represents a row of result data from an SQL query.


$rows[$i]['MYNAME'] is a string value containing the MYNAME field of the 
($i
+ 1)th row of result data from an SQL query.


If having all the indexes bothers you, hide them!


$row0 = $rows[0];

echo $row0['MYNAME'];


This helped me understand multidimensional arrays way back when.  See, if
$row[0] = $rows[0], you should be able to swap them in the echo statement.


That is,

"echo $row0['MYAGE']"  is equivalent to  "echo $rows[0]['MYAGE']"


hope this all helps!

Nik



Thanks Nik, that explanation helped me a ton.

appreciate your time.

Elliott

  Return to Index