SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to reduce SQL queries?

    I know there's a way, but how do I reduce this script into one sql query?

    PHP Code:
    <?
    $dbcnx 
    mysql_connect('localhost','dbname','pass');
    mysql_select_db('dbname');

    $sql mysql_query("SELECT ID FROM jokes ORDER BY rand() LIMIT 1") or die(mysql_error());
    $result mysql_fetch_array($sql);
    extract($result);

    $sql mysql_query("SELECT joke FROM jokes WHERE ID='$ID'") or die(mysql_error());
    $result mysql_fetch_array($sql);
    extract($result);

    $sql mysql_query("SELECT joketype FROM jokes WHERE ID='$ID'") or die(mysql_error());
    $result mysql_fetch_array($sql);
    extract($result);

    $sql mysql_query("SELECT joketitle FROM jokes WHERE ID='$ID'") or die(mysql_error());
    $result mysql_fetch_array($sql);
    extract($result);
    ?>

  2. #2
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: How to reduce SQL queries?

    I think you can just use the following:
    Code:
    SELECT ID, joke, joketype, joketitle FROM jokes ORDER BY rand() LIMIT 1
    Let us know if you have any problems.

    Regards.

    Pedro

  3. #3
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it was something simple. lol.


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
  •