PHP Fetch array function problem

I have a database with two tables.

One of the tables contains a string 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 parameter contains the name of the string.

I kind of expected to get an array containing a number between 0 and 10, but instead I get an array with two elements:

array(
    [0] = 0
    [row_name] = 0
    .
    .
    . // AND SO ON
)

      

What is it.

I have never worked with these functions before and www.php.net has no examples that really help ...

+1


source to share


3 answers


I'm not really 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;

      

+6


source


You want to get all the results in one call. With your method, you have to loop the results as Paolo showed you. But it would be better to use PDO with fetchAll . If you're looking into PHP database connections, look into PDO .



+3


source


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 the array to get. It is persistent and can enter the following values: MYSQL_ASSOC, MYSQL_NUM and the default MYSQL_BOTH.

+1


source







All Articles