vote up 0 vote down
star

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...

flag
You should also free the result with mysql_free_result. – Allain Lalonde Dec 8 at 20:14
No Allain Lalonde. – OIS Dec 8 at 20:50
add comment

3 Answers:

vote up 5 vote down
check

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;
link|flag
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 Dec 8 at 20:14
add comment
vote up 2 vote down

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.

link|flag
add comment
vote up 1 vote down

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.

link|flag
This is the wrong answer. The problem is he only gets the first row without looping, and the first result is 0. – OIS Dec 8 at 20:05
add comment

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.