简体   繁体   中英

$row=mysql_fetch_array($result); only returns even rows

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

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM