...

View Full Version : Resolved Random Problem



Geodesic_D
02-11-2010, 07:22 PM
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
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!

MattF
02-11-2010, 07:27 PM
$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);

Geodesic_D
02-11-2010, 07:35 PM
Matt, I inserted that code you posted and now all it says is:

<blank> Stole My <blank>

MattF
02-11-2010, 07:44 PM
Post your updated code.

Geodesic_D
02-12-2010, 06:14 PM
<?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();
?>

Fou-Lu
02-12-2010, 06:17 PM
<?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, 0);
$word2 = mysql_result($result2, 0);

echo($word1 . " Stole My " . $word2);
mysql_close();
?>


I belive that mysql_result always needs a row.

Geodesic_D
02-12-2010, 09:54 PM
Thank you, Fou-Lu. That solved the problem.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum