Development Quick bit of php/mysql help please?

Discussion in 'Software' started by Theo, 25 Apr 2004.

  1. Theo

    Theo What's a Dremel?

    Joined:
    12 Jun 2003
    Posts:
    466
    Likes Received:
    0
    Just a quickie, I hope. I'm making a little website for a friends band from Argentina, and have made a quick, basic php script which allows them to post stuff on a news page from a html form. Here's the php script for entering into the database:

    Code:
    <?php 
    
    $title = $_POST["title"]; 
    $text = $_POST["text"];  
    
    $connection = mysql_connect("localhost","username","password") or die ("Couldn't connect to database server."); 
    
    $db = mysql_select_db("dbname" , $connection) or die("Couldn't select database."); 
    
    $sql = "insert into psychotoxic values ('$title', '$text')"; 
    
    
    $sql_result = mysql_query($sql,$connection) or die("Couldn't execute query"); 
    
    mysql_close($connection); 
    
    print "Information Accepted." 
    
    ?> 
    
    
    This works completely fine, not a problem.

    I have also made a script to extract 5 entries from the database, and place them in a html table:

    Code:
    <?php 
    
    include("logo.htm");
    $connection = mysql_connect("localhost","usernamei","password") or die ("Couldn't connect to database server."); 
    
    $db = mysql_select_db("dbname" , $connection) or die("Couldn't select database."); 
    
    $sql = 'SELECT `title` , `text` ';
    $sql .= 'FROM `psychotoxic` ';
    $sql .= 'WHERE 1 LIMIT 0, 5';
    
    $sql_result = mysql_query($sql,$connection) or die("Couldn't execute query"); 
    echo "<br><br><table border=0 table width=700>";
    
    while ($myrow = mysql_fetch_array($sql_result)) { 
        $title = $myrow["title"]; 
       $text = $myrow["text"]; 
       echo "
     <tr>
        <th>
         <strong>$title</strong></th>
    	<tr><td><h1>
          $text
        </h1></td>
      </tr>
    ";
    } 
    
    echo "</TABLE>"; 
    
    mysql_free_result($sql_result); 
    mysql_close($connection); 
    
    ?> 
    
    
    This also works great, but I need it to extract the 5 newest entries rather than the 5 entries that were first entered into the database. I also need it to put the newest entry first, then going down the list as the entries get older.

    I apologise for my English, bit tired at the moment :) If anyone could help me with figure out what I need to do, I'll greatly appreciate any comments/suggestions.
     
  2. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Give the table an ID column or if there already is one then:

    select ID as well as title and text then order by ID descending:

    SELECT id,title,text FROM psychotoxic ORDER by id DESC LIMIT 0,5

    :)
     
  3. Theo

    Theo What's a Dremel?

    Joined:
    12 Jun 2003
    Posts:
    466
    Likes Received:
    0
    Thanks chap, got it sorted. Much appreciated :)
     
  4. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74

Share This Page