no output

Discussion in 'PHP' started by promotingspace.net, Jul 16, 2007.

  1. #1
    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> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<? 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
     
    promotingspace.net, Jul 16, 2007 IP
  2. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    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...
     
    NoamBarz, Jul 16, 2007 IP
  3. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    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
     
    promotingspace.net, Jul 16, 2007 IP
  4. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    ecentricNick, Jul 17, 2007 IP
  5. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes that was the problem
     
    promotingspace.net, Jul 17, 2007 IP