PHP Help

Discussion in 'PHP' started by Pixel T., Jan 12, 2009.

  1. #1
    Hey guys,

    I'm looking to build a custom script for my website and I want to create a file called admin.php. There I will be able to enter something into an input box which saves in MySQL. I know how to do this so far. But the one thing I dont understand is how would I make it so that everytime I submitted something to the MySQL Database, it would be on top of the page and what I have submitted previously, would go right below it. Like 10 of these per page and if I have more then each page would have 10 .

    Anyone know any tutorials on this or can show me how to do this?

    Thanks,
     
    Pixel T., Jan 12, 2009 IP
  2. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    your query should be like this

    
    
    $query = "SELECT id, title, description FROM yourtable ORDER BY id DESC";
    
    
    PHP:
    order by .... desc
     
    baris22, Jan 12, 2009 IP
  3. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Just to add to baris22's point above, there's also the 'LIMIT' clause, which you can use for paging.
    So to get the first page your SQL query should end with something like:
    ... ORDER BY id DESC LIMIT 10;
    Code (markup):
    Second page:
    ... ORDER BY id DESC LIMIT 10 OFFSET 11;
    Code (markup):
     
    phper, Jan 12, 2009 IP
  4. livedating

    livedating Active Member

    Messages:
    161
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    83
    #4
    You could also use it like this:
    ... ORDER BY id DESC LIMIT 11, 10;
    Code (markup):
    (the syntax is: LIMIT offset, row_count OR LIMIT row_count)
     
    livedating, Jan 13, 2009 IP
  5. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #5
    Ok well I'm pretty new to PHP so I put this on my page and it didnt work.

    <?php include "base.php"; ?>

    <?php $query = "SELECT username FROM users ORDER BY id DESC"; ?>


    Should I put something after it?

    base.php connects to my database.

    I'm really confused on this. The table is called "users" and the usernames are under "usernames". I just want to make list all the usernames in order by id.

    Thanks,
     
    Pixel T., Jan 13, 2009 IP
  6. artflutter

    artflutter Member

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    This will print out a list of usernames, assuming that the base.php connects to the DB OK.

    
    <?php
    include ("base.php");
    $query = "SELECT username FROM users ORDER BY id ASC";
    <ul>
    while($row = mysql_fetch_array($query)) {
       echo "<li>" . $row['username'] . "</li>";
    }
    </ul>
    ?>
    
    Code (markup):
    As you are new to PHP/MySQL I reccomend reading the tutorials at w3schools. I found them very helpful when i was learning, and still do.
     
    artflutter, Jan 13, 2009 IP
  7. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    phper, Jan 13, 2009 IP
  8. hassanahmad1

    hassanahmad1 Active Member

    Messages:
    150
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #8
    yea, like this:

    
    include ("base.php");
    $query = "SELECT username FROM users ORDER BY id ASC";
    $result = mysql_query($query);
    
    echo "<ul>";
    while($row = mysql_fetch_array($result)) {
       echo "<li>" . $row['username'] . "</li>";
    }
    echo "</ul>";
    
    Code (markup):
     
    hassanahmad1, Jan 14, 2009 IP
  9. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #9
    Get this error.

    help
     
    Pixel T., Jan 14, 2009 IP
  10. rjd22

    rjd22 Peon

    Messages:
    63
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Could you maybe post how your database tables look like? It will be easier to help you then.
     
    rjd22, Jan 14, 2009 IP
  11. Pixel T.

    Pixel T. Well-Known Member

    Messages:
    1,205
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    170
    #11
    I got it to work. Thanks,
     
    Pixel T., Jan 14, 2009 IP
  12. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #12
    And you can always get the exact mysql error with:
    mysql_query(...)or die(mysql_error()); :)
     
    elias_sorensen, Jan 14, 2009 IP
  13. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #13
    the better way to check queries is to set a function which checks whether the query is correct or not. You can do so by using this function:

    
    function check_query($query) {
     if(!$query) {
        die("Database error: ".mysql_error());
      }
    }
    
    PHP:
    You can call this function after each mysql_query($query) statement like this:

    
    // you can define this function anywhere in the page.
    function check_query($query) {
     if(!$query) {
        die("Database error: ".mysql_error());
      }
    }
    
    $query = "SELECT username FROM users ORDER BY id ASC";
    $result = mysql_query($query);
    check_query($result);
    
    PHP:
    this function saves time for retyping the die thing..Hope this helps.
     
    khan11, Jan 15, 2009 IP