For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > December 2004 > some problem to add a mysql field value to an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author some problem to add a mysql field value to an array
SAN CAZIANO

2004-12-26, 8:55 am

how can i read a mysql table of two fields and test if the first field has a
value then add the second value to an array

I try this but it doesn't works

while ($row=mysql_fetch_array($result))
{
for ($i=0; $i< mysql_num_fields($result); $i++)
if ($row['CampoChiave']=='true')
$TempArray[ ]=$row['NomeCampo'];
}


Hilarion

2004-12-26, 8:55 am

What is the "for" loop for if you do not use it's index ($i)?

You are testing if CampoChiave has a string value of 'true'.
You wrote that you want to test if it has any value. You
may do it like this:

if (isset($row['CampoChiave']) && ($row['CampoChiave']!=''))
$TempArray[]=$row['NomeCampo'];


Hilarion

PS.: I do not use MySQL, so my suggestions are general PHP related
and may not be sufficient.


Oli Filth

2004-12-26, 8:55 am

SAN CAZIANO wrote:
> how can i read a mysql table of two fields and test if the first field has a
> value then add the second value to an array
>
> I try this but it doesn't works
>
> while ($row=mysql_fetch_array($result))
> {
> for ($i=0; $i< mysql_num_fields($result); $i++)
> if ($row['CampoChiave']=='true')
> $TempArray[ ]=$row['NomeCampo'];
> }
>
>


Hi,

* Use mysql_fetch_assoc(), not mysql_fetch_array().

* You don't need the for loop that tests agains mysql_num_fields()

* It's generally more efficient if your CampoChiave takes integer values
1 and 0, rather than string values "true" and "false".

So:

===== START PHP CODE =====

while ($row = mysql_fetch_assoc($result))
{
if ($row["CampoChiave"])
{
$TempArray[] = $row["NomeCampo"];
}
}

====== END PHP CODE ======

Hope this helps,
Oli
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com