tags:

views:

44

answers:

2

We have this code:

$rowArray;
$rowID = 1;
$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);

while($row = mysql_fetch_array($result)){    
        $rowArray[$rowID] = $row['idCentros'];   
        $rowID = $rowID +1;
    }  

$numrows returns 4 (the rows we have in that table)...but for an unkown reason the loop starts retrieving the 2º row next it retrieves the 4º row and then it ends the loop ($row =false). As we understand this is generic code and the table definition is like this:

column idcentros int(11)      pk notnull autoincremental
column nombre    mediumtext

What could be happening? Thanks in advance...

A: 

try this:

$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);
$rowArray = array();
while($row = mysql_fetch_array($result))
{
   array_push($rowArray,$row);
}
Dalen
With your code it now starts on the 3rd row and then it exits the loop (becouse there isnt a 5th row to retrieve...) It always skips one row after fetching and in this case starts in the 3rd row :S
Aitor Hoces Astorkiza
A: 

I don't see why the above code shouldn't work, but ... here's how I would do it:

$rowArray = array();
$query = "SELECT idCentros FROM centros";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);

while($row = mysql_fetch_row($result)){
        $rowArray[] = $row[0];
}

... I believe you have $rowID set to 1 just for visualisation later, but it's pointless - you should use HTML lists or some $counter++ variable for the output.

Narf