Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Random Problem

  1. #1
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Random Problem

    I'm trying to write a PHP script that selects two random words from a database. It's just for a little fun thing I have in mind.

    The code I am using is such:

    PHP Code:
    <?php
    mysql_connect
    ("IP:PORT""USERNAME""PASSWORD") or die("Unable to connect to database.");
    mysql_select_db("DBNAME") or die("Unable to select database");

    $result1=mysql_query("SELECT word1 FROM guildname ORDER BY RAND() LIMIT 1");
    $result2=mysql_query("SELECT word2 FROM guildname ORDER BY RAND() LIMIT 1");
    echo(
    $result1 " Stole My " $result2);
    mysql_close();
    ?>
    The only words I have in the database at the moment are "One" to "Ten". When I run the script, instead of something like:

    Three Stole My Two

    I get this:

    Resource id #3 Stole My Resource id #4

    I have no idea what is going wrong here, and I was hoping someone could provide the answer. I got the code snippets from a tutorial, and I followed said tutorial to the leter!
    Last edited by Geodesic_D; 02-12-2010 at 09:54 PM.
    Geodesic_D
    "I'd much rather be called GeodesicDragon."

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $result1=mysql_query("SELECT word1 FROM guildname ORDER BY RAND() LIMIT 1");
    $result2=mysql_query("SELECT word2 FROM guildname ORDER BY RAND() LIMIT 1");
    
    $word1 = mysql_result($result1);
    $word2 = mysql_result($result2);
    
    echo($word1 . " Stole My " . $word2);

  • #3
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Matt, I inserted that code you posted and now all it says is:

    <blank> Stole My <blank>
    Geodesic_D
    "I'd much rather be called GeodesicDragon."

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Post your updated code.

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    mysql_connect
    ("IP:PORT""USERNAME""PASSWORD") or die("Unable to connect to database.");
    mysql_select_db("DBNAME") or die("Unable to select database");

    $result1=mysql_query("SELECT word1 FROM guildname ORDER BY RAND() LIMIT 1");
    $result2=mysql_query("SELECT word2 FROM guildname ORDER BY RAND() LIMIT 1");

    $word1 mysql_result($result1);
    $word2 mysql_result($result2);

    echo(
    $word1 " Stole My " $word2);
    mysql_close();
    ?>
    Geodesic_D
    "I'd much rather be called GeodesicDragon."

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    <?php
    mysql_connect
    ("IP:PORT""USERNAME""PASSWORD") or die("Unable to connect to database.");
    mysql_select_db("DBNAME") or die("Unable to select database");

    $result1=mysql_query("SELECT word1 FROM guildname ORDER BY RAND() LIMIT 1");
    $result2=mysql_query("SELECT word2 FROM guildname ORDER BY RAND() LIMIT 1");

    $word1 mysql_result($result10);
    $word2 mysql_result($result20);

    echo(
    $word1 " Stole My " $word2);
    mysql_close();
    ?>
    I belive that mysql_result always needs a row.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Geodesic_D (02-12-2010)

  • #7
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you, Fou-Lu. That solved the problem.
    Geodesic_D
    "I'd much rather be called GeodesicDragon."


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •