2013-01-22 43 views
4

应该是非常基本的,但我不能让它工作。我有此代码来迭代一个mysqli的查询:mysqli_fetch_array虽然循环列

while($row = mysqli_fetch_array($result)) { 
    $posts[] = $row['post_id'].$row['post_title'].$row['content']; 
} 

它的工作原理和回报:

变量#1:(阵列,3片)↵ 0(字符串): “4testtest”( 9个字符) 1(String):“欢迎来到WordPress!欢迎使用WordPress。这是您的第一篇文章,编辑或删除它,然后开始写博客! (99个字符) 2(String):“2样本PageThis是一个示例页面,它不同于博客文章,因为它将保留在一个位置,并将显示在 您的网站导航(在大多数主题中)。 (161个字符)

问题是,它将所有三个colums放在一列中,所以我无法分开访问它们。

这例如:

0(字符串): “4testtest”(9个字符)

应当分开成4,测试,测试

当我这样做:

while($row = mysqli_fetch_array($result)) {    
    $posts['post_id'] = $row['post_id']; 
    $posts['post_title'] = $row['post_title']; 
    $posts['type'] = $row['type']; 
    $posts['author'] = $row['author']; 
} 

它只输出1行而不是所有三个...

任何帮助,非常感谢!

+0

你是如何“打印”的第二个选项哟试过? –

+0

'<?php foreach($ posts as $ post):?> \t \t \t \t <?php echo $ post_id; ?> <?php echo $ post_title; ?> <?php echo $ type; ?> <?php echo $ author; ?> <?php endforeach; ?> – Sebastian

+0

同时检查http://php.net/manual/en/mysqli-result.fetch-assoc.php和http://php.net/manual/en/mysqli-result.fetch中的程序while循环示例-array.php –

回答

16

从MySQL获取所有的值:

$post = array(); 
    while($row = mysql_fetch_assoc($result)) 
    { 
     $post[] = $row; 
    } 

然后,让每个值:

<?php 
    foreach ($posts as $row) 
     { 
      foreach ($row as $element) 
      { 
       echo $element."<br>"; 
      } 
     } 
?> 

呼应值。或者您可以通过$后变量

+0

虽然你的代码是最接近正确的,但你确实需要在命名中更加一致...... –

+0

确实是拼写错误 –

-2

每个元素试试这个:

$i = 0;  
    while($row = mysqli_fetch_array($result)) { 

      $posts['post_id'] = $row[$i]['post_id']; 
      $posts['post_title'] = $row[$i]['post_title']; 
      $posts['type'] = $row[$i]['type']; 
      $posts['author'] = $row[$i]['author']; 

     } 
    $i++; 
    } 

print_r($posts); 
-3

尝试......

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
+0

这不会提供问题的答案。要批评或要求作者澄清,在他们的帖子下留下评论 - 你总是可以评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你会能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment)。 – Parixit

+2

@Parixit看起来像对我的答案?不确定它是否正确,但这对于评论并不重要。 – Anders

3

我认为这将是你的输出结果的更简单的方法。

对不起,我自己的数据应该很容易替换。

$query = "SELECT * FROM category "; 

$result = mysqli_query($connection, $query); 


    while($row = mysqli_fetch_assoc($result)) 
    { 
     $cat_id = $row['cat_id']; 
     $cat_title = $row['cat_title']; 

     echo $cat_id . " " . $cat_title ."<br>"; 
    } 

这将输出:

  • - ID标题
  • -1加里
  • -2约翰
  • -3迈克尔斯
2

这一次是你的解。

$x = 0; 
while($row = mysqli_fetch_array($result)) {    
    $posts[$x]['post_id'] = $row['post_id']; 
    $posts[$x]['post_title'] = $row['post_title']; 
    $posts[$x]['type'] = $row['type']; 
    $posts[$x]['author'] = $row['author']; 
    $x++; 
} 
-1

双方将完美的作品在同时mysqli_fetch_array循环

while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) { 
    $posts[] = $row['post_id'].$row['post_title'].$row['content']; 
} 

(OR)

while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) { 
    $posts[] = $row['post_id'].$row['post_title'].$row['content']; 
} 

mysqli_fetch_array() - 有第二个参数$与resultType。

MYSQLI_ASSOC:取关联数组

MYSQLI_NUM:取数字数组

MYSQLI_BOTH:取两者缔合和数字数组。

+0

这是一个实时的例子,它完美地为上面提到的代码工作,那么为什么你会得到负面评价。如果你使用上面的代码,任何失败的手段只是报告我,这是理由,并给予否定。 – Elangovan