tags:

views:

602

answers:

3

I have a database with 2 tables.

One of the tables holds a row containing numbers, from 0 to 10.

In PHP, I do this:

$query = "SELECT ".$param." FROM issues WHERE ".$param." >=0";
$result = @mysql_query($query) or showError("query failed");
if (!($record = mysql_fetch_array($result))) return null;
return $record;

The $param holds the name of the row.

I kinda expected to get an array holding the number 0 to 10, but instead I get an array with 2 elements:

[0] = 0 [row_name] = 0

And that's it.

I've never worked with these functions before and www.php.net doesn't have any examples that really help...

+5  A: 

I'm not exactly sure what you are trying to achieve here, but I think what you want is:

// query...
$records = array();
while($r = mysql_fetch_array($result)) {
    $records[] = $r;
}
return $records;
Paolo Bergantino
thanks, I can work with this. Now I'm getting a 2 dimensional array. Each element has 2 sub-elements, so I simply select the first(either is good since they always have the same value)
Vordreller
+1  A: 

By default mysql_fetch_array returns both normal and associative arrays, so you can access the values by position ($result[0]) or by name ($result['user_name'])':

array mysql_fetch_array  ( resource $result  [, int $result_type  ] )

result_type: The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH.

Ken
This is the wrong answer. The problem is he only gets the first row without looping, and the first result is 0.
OIS
+2  A: 

You want to get all the results in one call. With your method you have to loop the results like Paolo showed you. But it might be better to use PDO with fetchAll. If you are learning PHP database connections, learn PDO.

OIS