Looping through fields in a row
Looping through fields in a row
am 13.09.2007 16:28:15 von Stacey
Hi All,
I am trying to display a mysql record on the screen. I would rather
not use specific field names in case the fields change, etc. So, I
just want to create a simple table with the field names down the first
column and the corresponding values in the second. I've tried several
different snippets of code that I found, but I can't seem to get it
working right.
This is what I've been working off of:
$fieldNames=array_keys($myrow);
But this array have every other value as, what seems to be, a row id.
Looks like this:
Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
[5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
business_city)
Any suggestions would be appreciated.
Re: Looping through fields in a row
am 13.09.2007 16:44:02 von Captain Paralytic
On 13 Sep, 15:28, stacey wrote:
> Hi All,
>
> I am trying to display a mysql record on the screen. I would rather
> not use specific field names in case the fields change, etc. So, I
> just want to create a simple table with the field names down the first
> column and the corresponding values in the second. I've tried several
> different snippets of code that I found, but I can't seem to get it
> working right.
>
> This is what I've been working off of:
>
> $fieldNames=array_keys($myrow);
>
> But this array have every other value as, what seems to be, a row id.
> Looks like this:
>
> Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
> [5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
> business_city)
>
> Any suggestions would be appreciated.
use mysql_fetch_object or mysql_fetch_assoc and use a foreach thus
$rows = mysql_fetch_assoc($res);
foreach ($rows[0] as $field_name => $field_value)
echo "{$field_name} | {$field_value} |
";
Re: Looping through fields in a row
am 14.09.2007 14:16:56 von colin.mckinnon
On 13 Sep, 15:44, Captain Paralytic wrote:
> On 13 Sep, 15:28, stacey wrote:
>
>
>
> > Hi All,
>
> > I am trying to display a mysql record on the screen. I would rather
> > not use specific field names in case the fields change, etc. So, I
> > just want to create a simple table with the field names down the first
> > column and the corresponding values in the second. I've tried several
> > different snippets of code that I found, but I can't seem to get it
> > working right.
>
> > This is what I've been working off of:
>
> > $fieldNames=array_keys($myrow);
>
> > But this array have every other value as, what seems to be, a row id.
> > Looks like this:
>
> > Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
> > [5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
> > business_city)
>
> > Any suggestions would be appreciated.
>
> use mysql_fetch_object or mysql_fetch_assoc and use a foreach thus
>
> $rows = mysql_fetch_assoc($res);
> foreach ($rows[0] as $field_name => $field_value)
> echo "{$field_name} | {$field_value} |
";
Thanks for playing Captain, unfortunately the correct answer was:
while ($row = mysql_fetch_assoc($res)) {
foreach ($row as $name => $val) {
echo "{$name} | {$val} |
\n";
}
}
C.
Re: Looping through fields in a row
am 14.09.2007 15:42:54 von luiheidsgoeroe
On Thu, 13 Sep 2007 16:28:15 +0200, stacey wrote:=
> Hi All,
>
> I am trying to display a mysql record on the screen. I would rather
> not use specific field names in case the fields change, etc. So, I
> just want to create a simple table with the field names down the first=
> column and the corresponding values in the second. I've tried several
> different snippets of code that I found, but I can't seem to get it
> working right.
>
> This is what I've been working off of:
>
> $fieldNames=3Darray_keys($myrow);
>
> But this array have every other value as, what seems to be, a row id.
> Looks like this:
>
> Array ( [0] =3D> 0 [1] =3D> sheet_id [2] =3D> 1 [3] =3D> client_id [4]=
=3D> 2
> [5] =3D> business_name [6] =3D> 3 [7] =3D> business_address [8] =3D> 4=
[9] =3D>
> business_city)
Don't fetch the result with mysql_fetch_array(), use mysql_fetch_assoc()=
=
instead. mysql_fetch_array() will return both a numerical as named array=
=
by default.
-- =
Rik Wasmus
Re: Looping through fields in a row
am 14.09.2007 16:38:40 von Captain Paralytic
On 13 Sep, 15:44, Captain Paralytic wrote:
> On 13 Sep, 15:28, stacey wrote:
>
>
>
>
>
> > Hi All,
>
> > I am trying to display a mysql record on the screen. I would rather
> > not use specific field names in case the fields change, etc. So, I
> > just want to create a simple table with the field names down the first
> > column and the corresponding values in the second. I've tried several
> > different snippets of code that I found, but I can't seem to get it
> > working right.
>
> > This is what I've been working off of:
>
> > $fieldNames=array_keys($myrow);
>
> > But this array have every other value as, what seems to be, a row id.
> > Looks like this:
>
> > Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
> > [5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
> > business_city)
>
> > Any suggestions would be appreciated.
>
> use mysql_fetch_object or mysql_fetch_assoc and use a foreach thus
>
> $rows = mysql_fetch_assoc($res);
> foreach ($rows[0] as $field_name => $field_value)
> echo "{$field_name} | {$field_value} |
";- Hide quoted text -
>
> - Show quoted text -
Yes of course it was!
I have spent so long using mysql through a framework, that I totally
forgot what the raw functions do!
Re: Looping through fields in a row
am 15.09.2007 01:15:16 von Norman Peelman
Rik Wasmus wrote:
> On Thu, 13 Sep 2007 16:28:15 +0200, stacey wrote:
>
>> Hi All,
>>
>> I am trying to display a mysql record on the screen. I would rather
>> not use specific field names in case the fields change, etc. So, I
>> just want to create a simple table with the field names down the first
>> column and the corresponding values in the second. I've tried several
>> different snippets of code that I found, but I can't seem to get it
>> working right.
>>
>> This is what I've been working off of:
>>
>> $fieldNames=array_keys($myrow);
>>
>> But this array have every other value as, what seems to be, a row id.
>> Looks like this:
>>
>> Array ( [0] => 0 [1] => sheet_id [2] => 1 [3] => client_id [4] => 2
>> [5] => business_name [6] => 3 [7] => business_address [8] => 4 [9] =>
>> business_city)
>
> Don't fetch the result with mysql_fetch_array(), use mysql_fetch_assoc()
> instead. mysql_fetch_array() will return both a numerical as named array
> by default.
> --Rik Wasmus
Or just:
mysql_fetch_array($result,MYSQL_ASSOC); // MYSQL_ASSOC, MYSQL_NUM,
MYSQL_BOTH are allowed. Default is MYSQL_BOTH.
Norm