Forum Moderators: coopster

Message Too Old, No Replies

Newbie Array question

         

ekim_mike

1:34 pm on Nov 6, 2003 (gmt 0)

10+ Year Member



Hey everybody,
I know enough to be dangerous and I'm trying to get a handle on some of the stuff I'm doing.
ok so I understand how to create an array:

array(data1, data2, data3,);
array[0] = data1
array[1] = data2
array[2] = data3

My question is when I do:
(saying the table has 4 cols)
$row = mysql_fetch_array($result)
echo $row['col1']
echo $row['col2']
echo $row['col3']
echo $row['col4']

how does a row of cols sit in the array?
What would the data structure be?
would it be:
$row[0] = col1 => col2 => col3 =>col4
and would that be considered a Multi-dimensional array?

NickCoons

1:54 pm on Nov 6, 2003 (gmt 0)

10+ Year Member



ekim_mike,

I usually use associative arrays for MySQL queries, so I'm not exactly sure. But I believe it would look more like this:

$row[0] = col1
$row[1] = col2
$row[2] = col3
$row[3] = col4

If you use the following:

$row = mysql_fetch_array($result, MYSQL_ASSOC)

...instead of:

$row = mysql_fetch_array($result)

...then you can refer to each column this way, as you've demonstrated with echo():

$row['col1'] = col1
$row['col2'] = col2
$row['col3'] = col3
$row['col4'] = col4

jatar_k

4:44 pm on Nov 6, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld ekim_mike,

and would that be considered a Multi-dimensional array?

You can't consider it a multi-dimensional array unless you did something like this

$row[] = mysql_fetch_array($result)

this would put each row into the next slot in the array $row and once you retrieved all rows from the db you would end up with an array filled with arrays. The array would look very similar to the original table. Then to access the values for the first row you would do

echo $row[0][0]
echo $row[0][1]
echo $row[0][2]
echo $row[0][3]

The strange thing about mysql_fetch_array is that, without the flag, you can access the array in two ways. You can either use the column name or the numeric index

[ca.php.net...]

The optional second argument result_type in mysql_fetch_array() is a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. This feature was added in PHP 3.0.7. MYSQL_BOTH is the default for this argument.

By using MYSQL_BOTH, you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

ekim_mike

7:42 pm on Nov 6, 2003 (gmt 0)

10+ Year Member



Thanks for the help!