如何增加mysql_fetch_array指针?

时间:2010-08-08 08:51:03

标签: php mysql

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
       $row = mysql_fetch_array($result, MYSQL_ASSOC) //it's not increase ?
}

我想在每个循环中增加两次?

<table>
<td>**1 times**</td><td>**1 times**</td>
</table>

6 个答案:

答案 0 :(得分:1)

我认为文档很清楚。 http://hu.php.net/manual/en/function.mysql-fetch-array.phphttp://hu.php.net/manual/en/function.mysql-fetch-assoc.php

    mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %s  Name: %s", $row[0], $row[1]);  
    }

mysql_free_result($result);

答案 1 :(得分:1)

你不需要那个。 如果要打印格式为2列的数据,请将其全部选择为数组,然后将该数组用于格式化输出。

可能的解决方案之一

<?php
//collect data into array
$data = array();
while ($row = mysql_fetch_assoc($result)) {
  $data[] = $row;
}
//and here goes template part
?>
<html>
<? $data = array_chunk($data, 2) ?>
<table>
<? foreach ($data as $chunk): ?>
  <tr>
<? foreach ($chunk as $row): ?>
    <td><?=$row['name']?></td>
<? endforeach ?>
  </tr>
<? endforeach ?>
</table>

答案 2 :(得分:0)

它会将指针增加1步......

因此:

 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      // logic here
   }

将执行循环和逻辑

答案 3 :(得分:0)

为什么不这样做:

$max_row = 100;//mysql_num_rows($result);
for($idx = 0; $idx < $max_row; $idx+=2){
if (!mysql_data_seek($result, $i)) {
    echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (!($row = mysql_fetch_assoc($result))) {
        continue;
    }

    //do what you want to do here...
}

如果你有10条记录,它将回显第0,2,4,6,8行。

答案 4 :(得分:0)

<?php
$counter=0;

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
       if ($counter % 2)
       {
           echo '<table><tr>'; // start table
       }

       echo '<td>' . $row['foo'] . '</td>'; // echo result

       if (!$counter % 2)
       { 
          echo "</tr></table>"; // end table
       }
       $counter++;
}
?>

答案 5 :(得分:-1)

也许你想要mysql_data_seek