mysql - Trying to fetch all data with a while loop but the first data is already fetched, don't know how to change my code. (Php) -


update 2 - fixed:

i figured out how fix myself after long hard think haha it's easy way might call ugly works me!

i've created variable $row , called $post:

$post = $row

and used other variable show other stuff of article.

update:

thanks niels helping me out! facing problem if try echo code after while loop won't appear if in front of it show nicely? how can fix issue? thanks


my question

so here's example code use show categories linked postid. example post 1 has categories: apple, green , yellow linked it.

but can't seem fetch data correctly since has been fetched once @ top can't proper while loop @ categories: part of code try while loop. while loop works , fetches categories except first 1 , when place while loop

$row['posttitle']

and

$row['postcont']

won't appear anymore because it's being skipped. how fix this? thanks.


<?php require('includes/config.php');   $stmt = $db->prepare("  select *                          blog_posts                          left join  blog_posts_categories on blog_posts.postid=blog_posts_categories.postid                          inner join blog_categories on blog_posts_categories.catid=blog_categories.catid                          blog_posts.postid = :postid");  $stmt->execute(array(':postid' => $_get['id'])); $row = $stmt->fetch();  //if post not exists redirect user homepage. if($row['postid'] == ''){     header('location: ./');     exit; }  ?> <!doctype html> <html lang="en"> <head>     <meta charset="utf-8">     <title><?php echo $row['posttitle'];?> | website</title>     <link rel="stylesheet" href="style/normalize.css">     <link rel="stylesheet" href="style/main.css"> </head> <body>      <div id="wrapper">          <h1>single post page</h1>         <hr />         <p><a href="./">home</a> |               categories:               <?php while($row = $stmt->fetch())             {                 //the first category being skipped? how fix?                 echo $row['catname'];             } ?>          </p>         <div>             <?php                  //these won't appear because of while loop. it's being skipped.                 echo '<h1>'.$row['posttitle'].'</h1>';                 echo '<p>'.$row['postcont'].'</p>';              ?>         </div>      </div>  </body> </html> 

replace:

        while($row = $stmt->fetch())         {             echo $row['catname'];         } 

with:

        {             echo $row['catname'];         } while($row = $stmt->fetch()); 

as other items - put them inside loop instead of afterwards.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -