Hi I'm trying to list the articles in section 105 and use this link: http://127.0.0.1/Takfekr/htdocs/section.php?id=105 with this code: section.php: <?php include 'global.php'; if (isset($_GET['id'])) { $section_id = $_GET['id']; $sql = "SELECT * FROM articles WHERE section = '$section_id' ORDER BY id"; $results = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_array($results); while($row = mysql_fetch_array( $results )) { $title=$row['title']; $text=$row['text']; $username=$row['username']; $name=$row['name']; ?> <div class="article_header"> <h1 dir="rtl"> <?php echo "hello!"; echo $title ;?></h1> <div class="asd1"> </div> <div style="float: right;" dir="rtl"> <span dir="rtl">writer: <br> <? echo $name; ?></a></span> </div> <br> </div> <br> <!-- ARTICLE CONTENT START --> <p class="MsoNormal" dir="rtl"><?php echo $text; ?> </p> <br><p class="MsoNormal" dir="rtl"> <?php } ?> <o:p></o:p></p> <!-- ARTICLE COMMENTS STOP --> </div> </div> <?php } else header('Location: account.php'); ?> PHP: and section 105 is not empty. it has 1 record because when I use: $query = "SELECT Count(*) FROM articles WHERE section='$section_id'"; $result = mysql_query($query) or die(mysql_error()); echo 'count: ', mysql_result($result, 0), "<br />\n"; PHP: It sends: count: 1 what's the problem? thanks in advance
Use this: while($row = mysql_fetch_array( $results )) { $title=$row['title']; $text=$row['text']; $username=$row['username']; $name=$row['name']; echo "<div class=\"article_header\">"; echo "<h1 dir=\"rtl\">"; echo "hello! $title </h1>"; echo "<div class=\"asd1\"> </div>"; echo "<div style=\"float: right;\" dir=\"rtl\">"; echo "<span dir=\"rtl\">writer: "; echo "<br> "; echo "$name</a></span></div><br></div>"; echo "<br><p class=\"MsoNormal\" dir=\"rtl\"> $text </p><br>"; echo "<p class=\"MsoNormal\" dir=\"rtl\">"; } this should work...
now this is the full code: <?php include 'global.php'; if (isset($_GET['id'])) { $section_id = $_GET['id']; $sql = "SELECT * FROM articles WHERE section = '$section_id' ORDER BY id"; $results = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_array($results); while($row = mysql_fetch_array( $results )) { $title=$row['title']; $text=$row['text']; $username=$row['username']; $name=$row['name']; echo "<div class=\"article_header\">"; echo "<h1 dir=\"rtl\">"; echo "hello! $title </h1>"; echo "<div class=\"asd1\"> </div>"; echo "<div style=\"float: right;\" dir=\"rtl\">"; echo "<span dir=\"rtl\">writer: "; echo "<br> "; echo "$name</a></span></div><br></div>"; echo "<br><p class=\"MsoNormal\" dir=\"rtl\"> $text </p><br>"; echo "<p class=\"MsoNormal\" dir=\"rtl\">"; } $query = "SELECT Count(*) FROM articles WHERE section='$section_id'"; $result = mysql_query($query) or die(mysql_error()); echo 'count: ', mysql_result($result, 0), "<br />\n"; ?> <o:p></o:p></p> <!-- ARTICLE COMMENTS STOP --> </div> <iframe name="ifrRate" frameborder="0" height="0" width="0"></iframe> </div> <?php } else header('Location: account.php'); ?> PHP: but the only output is: count: 1
You have *two* fetches... $row = mysql_fetch_array($results); while($row = mysql_fetch_array( $results )) { So, the first fetch gets your 1st record, and the second fetch probably fails, clearing out $row? You don't need the first one