+ Reply to Thread
Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By garrettroyce
  • 1 Post By IonCannon218

Thread: How do I only select the last 5 rows of a table in a database?

  1. #1
    IonCannon218's Avatar
    IonCannon218 is offline x10 Sophmore IonCannon218 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    177

    How do I only select the last 5 rows of a table in a database?

    I'm playing with a Program E for ALICE, I want the PHP script to display the last 5 lines in the conversation.

    MySQL info: The database is 'alice', the table i want to get rows from is 'conversationlogs'. the columns to display are 'input', 'response' and 'uid'. I want the script to only display the log from the user id.
    The script works completely fine, but I don't want the script to show the complete logs from everyone.

    The particular code i'm asking for help is:
    PHP Code:
    // Print the last 5 lines in the conversation
     
    $query="SELECT * FROM conversationlog";
     
    $result mysql_query($query);
     while(
    $row mysql_fetch_array($result))
     {
      echo 
    '<b>You: </b>' $row['input'] . '<br>';
      echo 
    '<b>IonBot: </b>' $row['response'] . '<br>';
     }
     echo 
    '<b>You: </b>' $HTTP_POST_VARS['input'] . '<br>'
    The full script: (talk.php)
    PHP Code:
    <?php
    /*
        Program E
     Copyright 2002, Paul Rydell
     This file is part of Program E.
     
     Program E is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.
        Program E is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
        You should have received a copy of the GNU General Public License
        along with Program E; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */
    /**
     * HTML chat interface
     * 
     * Contains the script that outputs the HTML interface for chatting
     * @author Paul Rydell
     * @copyright 2002
     * @version 0.0.8
     * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     * @package Interpreter
     * @subpackage Responder
     */
     
    /**
    * Include the guts of the program.
    */
    include "respond.php";
    if (isset(
    $HTTP_POST_VARS['input'])){
     
    $numselects=0;
     
    // Start the session or get the existing session.
     
    session_start();
     
    $myuniqueid=session_id();
     
    // Print the last 5 lines in the conversation
     
    $query="SELECT * FROM conversationlog";
     
    $result mysql_query($query);
     while(
    $row mysql_fetch_array($result))
     {
      echo 
    '<b>You: </b>' $row['input'] . '<br>';
      echo 
    '<b>IonBot: </b>' $row['response'] . '<br>';
     }
     echo 
    '<b>You: </b>' $HTTP_POST_VARS['input'] . '<br>';
     
     
    // Here is where we get the reply.
     
    $botresponse=replybotname($HTTP_POST_VARS['input'],$myuniqueid,$HTTP_POST_VARS['botname']);
     
    // Print the results.
     
    print "<br>";
     print 
    "<B>IonBot: " $botresponse->response "<BR></b><br>";
     
    //print "<BR><BR>execution time: " . $botresponse->timer;
     //print "<BR>numselects= $numselects";
     //print_r($botresponse->inputs);
     //print_r($botresponse->patternsmatched);
     // Include a form so they can say more. Note the hidden part for people that do not have trans sid on but want non-cookie users to be able to use the system.
     
     
     
    ?>
     <html>
     <head>
     <title>Chat with IonBot</title>
     </head>
     <body>
     <form name="form1" method="post" action="talk.php">
     <input type="hidden" name="<?=session_name()?>" value="<?=$uid?>">
     <input type="hidden" name="botname" value="<?=$HTTP_POST_VARS['botname']?>">
       Say: <input type="text" name="input" size="55">
       <input type="submit" name="Submit" value="Submit">
     </form>
     </body>
     </html>
    <?
    }
    else {
     
    $availbots=array();
     
    // Get all the names of our bots.
     
    $query="select botname from bots";
        
    $selectcode mysql_query($query);
        if (
    $selectcode){
            if(!
    mysql_numrows($selectcode)){
            }
            else{
                while (
    $q mysql_fetch_array($selectcode)){
                    
    $availbots[]=$q[0];
                }
            }
        }
     
    ?>
     <html>
     <head>
     <title>Chat with IonBot</title>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
     </head>
     <body bgcolor="#FFFFFF" text="#000000">
     <form name="form1" method="post" action="talk.php">
     Talk to: <select name="botname">
     <? 
     
    foreach ($availbots as $onebot){
      print 
    "<option value=\"$onebot\">$onebot</option>";
     }
     
    ?>
     </select><BR>
       Input: <input type="text" name="input" size="55">
       <input type="submit" name="Submit" value="Submit">
     </form>
     
     </body>
     </html>
     <?
    }
    ?>
    Formerly bnex10

  2. #2
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    Re: How do I only select the last 5 rows of a table in a database?

    Try this for your SQL.

    SELECT * FROM conversationlog LIMIT 5

  3. #3
    akkudreamz's Avatar
    akkudreamz is offline x10 Sophmore akkudreamz is an unknown quantity at this point
    Join Date
    Jan 2009
    Location
    Jaipur, Rajasthan, India
    Posts
    183

    Re: How do I only select the last 5 rows of a table in a database?

    ya
    and use ORDER BY to order according to the time the chats were sent and put a limit of 5
    Be safety conscious. 80% of people are caused by accidents.
    My Weblog

  4. #4
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: How do I only select the last 5 rows of a table in a database?

    if you only want three columns, you can specify that too:

    SELECT input, response, uid
    FROM conversationlogs
    ORDER BY time DESC
    LIMIT 5
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  5. #5
    IonCannon218's Avatar
    IonCannon218 is offline x10 Sophmore IonCannon218 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    177

    Re: How do I only select the last 5 rows of a table in a database?

    Thanks, how do I do the uid checking in php code?

    I only want the rows that match the uid of the client.
    Formerly bnex10

  6. #6
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: How do I only select the last 5 rows of a table in a database?

    "SELECT input, response, uid
    FROM conversationlogs
    WHERE uid = $uid
    ORDER BY time DESC
    LIMIT 5"

    you can call $uid whatever you want, just make sure it contains a valid uid
    karimirt47 likes this.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  7. #7
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: How do I only select the last 5 rows of a table in a database?

    You can remove the uid field from the query, as you already have it's value.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  8. #8
    IonCannon218's Avatar
    IonCannon218 is offline x10 Sophmore IonCannon218 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    177

    Re: How do I only select the last 5 rows of a table in a database?

    Yeah, I got it working the way i wanted to do now.
    dinomirt96 likes this.
    Formerly bnex10

+ Reply to Thread

Similar Threads

  1. I can't select the database!
    By offres in forum Free Hosting
    Replies: 1
    Last Post: 04-02-2008, 07:50 PM
  2. I can't select the database!
    By offres in forum Free Hosting
    Replies: 1
    Last Post: 04-02-2008, 02:24 PM
  3. install drupal
    By abhi666 in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 02-18-2008, 08:05 AM
  4. PHP BB 2.0.16 Manual instalation
    By GFIV in forum Free Hosting
    Replies: 8
    Last Post: 09-14-2005, 12:40 PM
  5. Table inside table
    By wizeman in forum Tutorials
    Replies: 4
    Last Post: 07-11-2005, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers