some problem to add a mysql field value to an array

This is a discussion on some problem to add a mysql field value to an array within the alt.comp.lang.php forums, part of the PHP Programming Forums category; how can i read a mysql table of two fields and test if the first field has a value then ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-21-2004
SAN CAZIANO
 
Posts: n/a
Default some problem to add a mysql field value to an array

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'];
}


Reply With Quote
  #2 (permalink)  
Old 12-21-2004
Hilarion
 
Posts: n/a
Default Re: some problem to add a mysql field value to an array

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.


Reply With Quote
  #3 (permalink)  
Old 12-21-2004
Oli Filth
 
Posts: n/a
Default Re: some problem to add a mysql field value to an array

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:05 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0