tags:

views:

96

answers:

5
  $result = mysql_query("SELECT position
     FROM ".$this->table_name."
     WHERE c_name = '".$chName."'");

     while($row = mysql_fetch_array($result)) 
     {
      return(array($row['position']));
     }

I am getting the same value, even if i change the combox... i need the ID of the combox box when its changed. But for every time i select it displays the same value.

function getID($chName) {

}

chName is the value i am getting from combo box from Flex.

A: 

why

 return(array($row['position']));

and not just

return($row['position']);

For any other help you have to provide more informationn (hmtl? output?)

Flo
+2  A: 

You are always returning just the first row. Maybe you want the whole data set and not just the first record. So try this:

$result = mysql_query("SELECT position
    FROM ".$this->table_name."
    WHERE c_name = '".$chName."'");
$retVal = array();
while ($row = mysql_fetch_array($result)) {
    $retVal[] = $row['position']);
}
return $retVal;
Gumbo
A: 

Gumbo's answer is correct.

Notice that unconditionally returning something inside a loop makes the loop itself to be run only once. This is true in general and you should always avoid such a monster :)

If you are a memory saver guy you do a very little memory an CPU improvement:

mysql_fetch_assoc()

that avoids retrieving an integer indexed array you'll not use

AlberT
A: 

Dont use double quotes if you arent wanting variables inside parsed.

$results = array();

// Dont forget you should be escaping $chName
$result = mysql_query("SELECT `position` FROM `{$this->table_name}` WHERE `c_name` = '$chName';");

while($row = mysql_fetch_array($result)) 
{
 $results[] = $row['position'];
}

return $results;

Or

$results = array();

// Dont forget you should be escaping $chName
$result = mysql_query('SELECT `position` FROM `'.$this->table_name.'` WHERE `c_name` = \''.$chName.'\';');

while($row = mysql_fetch_array($result)) 
{
 $results[] = $row['position'];
}

return $results;
A: 

The other folks have posted good answer; Just one suggestion - code a DB wrapper class so you don't have to manually fetch arrays all the time, which is a source of constant error. I have my own Query class and after performing a query, I just do $query->dumpResultAsArray() and do the for-each from there.

It's less efficient, but it's more robust and easier to debug.

Extrakun