WEB outputting database data

Discussion in 'OT Technology' started by BoostedBoogie, Jun 3, 2008.

  1. so i have a database with 3 fields (id, name, comment). i simply want to display the name and comment by id desc. this is the code i have:

    Code:
    <?php
    include 'includes/config.php';
    include 'includes/opendb.php';
    
    $query = "SELECT * FROM bulletins ORDER BY id desc";
    $sql = mysql_query($query) or die (mysql_error());
    
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    	echo $row;
    }
    ?>
    
    now i'm 99% sure that the echo $row is incorrect. i just can't figure out how to display the result information. also do i need to echo the information in a table to organize everything? for example, echo "<tr><td>$name</td></tr> if that name var was set to the name field content?
     
  2. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    look at the other examples on w3schools
     
  3. intrktevo

    intrktevo New Member

    Joined:
    Oct 18, 2004
    Messages:
    5,781
    Likes Received:
    0
    Location:
    UCF
    row is an array with the items named by what the mysql query returns

    Code:
    <?php
    include 'includes/config.php';
    include 'includes/opendb.php';
    
    $query = "SELECT * FROM bulletins ORDER BY id desc";
    $sql = mysql_query($query) or die (mysql_error());
    
    echo "<table><tr><th>Name</th><th>Comment</th></tr>";
    
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
        echo "<tr><td>{$row['name']}</td><td>{$row['comment']}</td></tr>";
    }
    
    echo "</table>";
    ?>
    
     
  4. Logik

    Logik Livin la vida broka

    Joined:
    Jun 30, 2000
    Messages:
    20,667
    Likes Received:
    1
    Location:
    The Steel City
  5. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    :ugh:

    intrktevo is right.
     
  6. Logik

    Logik Livin la vida broka

    Joined:
    Jun 30, 2000
    Messages:
    20,667
    Likes Received:
    1
    Location:
    The Steel City
    i know, hence the emoticon. :hsd:
     
  7. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    :squint:

    :hug:
     
  8. thanks, got that working!
     
  9. Josh

    Josh Guest

    Here is another way to do it. This makes things a bit easier when you hate typing the row array.

    Code:
    
    <?php
    include 'includes/config.php';
    include 'includes/opendb.php';
    
    $query = "SELECT `id`,`name`,`comment` FROM `bulletins` ORDER BY `id` DESC";
    $sql = mysql_query($query) or die (mysql_error());
    
    echo "<table><tr><th>Name</th><th>Comment</th></tr>";
    
    while(list($id,$name,$comment) = mysql_fetch_row($result, MYSQL_NUM))
    {
        echo "<tr><td>{$name}</td><td>{$comment}</td></tr>";
    }
    
    echo "</table>";
    
    
    ?>
    
    
    Basically the same end result, but sweet little trick that will save you some time down the road. And I think it is bad practice to use the * unless you're really going to use every field's data.
     
  10. i actually am going to use all of the data but was going to limit the results...or should i avoid using * altogether? i figure down the road, there will be hundreds of entries that i'm selecting but only limiting 20.
     
  11. Josh

    Josh Guest

    the * means what columns you are selecting (in this case, * means select all columns) . So if you are only choosing 20 rows you would do that like this:

    $query = "SELECT `id`,`name`,`comment` FROM `bulletins` ORDER BY `id` DESC LIMIT 0, 20";
     
  12. Josh

    Josh Guest

    Also in my previous code I think for fetch_row you would remove the MYSQL_NUM so it would be just

    mysql_fetch_row($result)
     
  13. ahhh ok, i gotcha. i'll give that option a try too. before i always used to select everything. i guess this way just keeps it from running slow by selecting unnecessary data that i won't use?
     
  14. Josh

    Josh Guest

    Basically, and if you don't intend on using every row in the table, which you likely hardly ever will, use the LIMIT clause. It works like this

    LIMIT x, y

    x is the record to start at, 0 being the first
    y is the number of records to return
     

Share This Page