Results 1 to 3 of 3

Thread: echo result from db

  1. #1
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default echo result from db

    Any ideas?

    I am trying to echo results from the DB but I am only seeing the first line. I have more than 1 record in the db?

    Any help or suggestions would be great, Thanks.


    Code:
    <?php
    // query db and echo from database
        $sql = "SELECT * FROM `comments` ORDER BY ID ASC"; 
        if ($result = mysql_query($sql)) {
          if (mysql_num_rows($result)) {
            $row = mysql_fetch_assoc($result);
    
            echo "<h2>" . $row['Name'] . "</h2>"; // 
     		echo "<h2>" . $row['Time'] . "</h2>"; //
    		echo "<p>" . $row['Comment'] . "</p>"; // 
    
    		} else {
    		echo "Something is wrong!";
          }
        }
    ?>
    
    <div class="comment">
    
    
    <?php
    
    //initilize PHP
    
    if($_POST['submit']) //If submit is hit
    {
    
       //convert all the posts to variables:
       $name = $_POST['name'];
       $email = $_POST['email'];
       $comment = $_POST['comment'];
       
       
       //Insert the values into the correct database with the right fields
       //mysql table = news
       //table columns = id, title, message, who, date, time
       //post variables = $title, $message, '$who, $date, $time
       $result=MYSQL_QUERY("INSERT INTO comments (ID,Name,Email,Time,Comment)".
          "VALUES ('NULL', '$name', '$email', 'NULL', '$comment')"); 
    
        //confirm
       echo "Query Finished"; 
    }
    
    
    ?>
    
        <form id="sendcomment" name="sendcomment" method="post" action="">
    
          <?php
    	//if (isset($missing) && in_array('name', $missing)) { ?>
          <span class="warning">Enter your name.</span>
          <?php //} ?>
    
          <label><abbr title="Enter your name."><font color="#FF0000"><sup>*</sup></font>Name:</abbr>
            <input type="text" name="name" id="name" maxlength="35" />
          </label>
    
          <?php
    //	if (isset($missing) && in_array('email', $missing)) { ?>
          <span class="warning">Enter your email address.</span>
          <?php //} ?>
    
          <label><abbr title="Enter your email address."><font color="#FF0000"><sup>*</sup></font>Email:</abbr>
            <input type="text" name="email" id="email" maxlength="40"/>
          </label>
    
          <?php
    	//if (isset($missing) && in_array('tel', $missing)) { ?>
          <span class="warning">Enter your contact no.</span>
          <?php //} ?>
    
       <!-- <label><abbr title="Enter your contact no."><font color="#FF0000"><sup>*</sup></font>Tel.:</abbr>
            <input type="text" name="time" id="time" maxlength="25" /></label> -->
    
    	  <label><abbr title="Enter your comment."><font color="#FF0000"><sup>*</sup></font>Your Comment:</abbr></label>
    
      	 <textarea name="comment" id="comment" cols="0" rows="7"></textarea>
    
            <input type="submit" name="submit" id="submit" class="submit" value="Send" title="Send" />
    
            <input type="reset" name="reset" id="reset" class="reset" value="Reset" title="Reset" />
    
        </form>
    Last edited by Snookerman; 08-10-2009 at 02:16 PM.

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    You need to do a while loop to loop through the records, add this:


    Replace:

    PHP Code:
    $row mysql_fetch_assoc($result);

    echo 
    "<h2>" $row['Name'] . "</h2>"//
    echo "<h2>" $row['Time'] . "</h2>"//
    echo "<p>" $row['Comment'] . "</p>"//


    With:
    PHP Code:
    while($row mysql_fetch_assoc($result)) {
      echo 
    "<h2>" $row['Name'] . "</h2>"//
      
    echo "<h2>" $row['Time'] . "</h2>"//
      
    echo "<p>" $row['Comment'] . "</p>"//
    } } 

  3. The Following User Says Thank You to Schmoopy For This Useful Post:

    john0611 (08-10-2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Hay thanks Schmoopy! That did the trick.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •