0

I'm working on a hangman game but I want to select a random word from my database.

The database connection:

     $db_host               = "127.0.0.1"; 
     $db_user               = "root";
     $db_pass               = "";
     $db_table          = "galgje";

    $db = mysql_connect($db_host,$db_user,$db_pass);
    mysql_select_db($db_table,$db);
    $query = "SELECT word FROM dba_tblwords " ;
    $select = @mysql_query($query);

while($row = mysql_fetch_array(($select)))

{
$list = ($row['word']);
}

Select a new word:

 if ($wrong >= $max)
 {
SELECT word FROM dba_tblwords ORDER BY RAND() LIMIT 0,1  
} 

This isnt working. Who can help me?

  • You have to put the SQL query in a string and use the `*_query()` function just like in your existing code. – mario Jul 02 '15 at 20:51
  • Why are you not passing the query to database. The problem is not visible from your this partial code. Some code relevant to the problem may be missing in your problem statement – Md Johirul Islam Jul 02 '15 at 20:55
  • In other news: [Why shouldn't I use mysql\_\* functions in PHP?](http://stackoverflow.com/q/12859942) – mario Jul 02 '15 at 21:15

1 Answers1

0

You should use this query

SELECT word FROM dba_tblwords ORDER BY RAND() LIMIT 1

And pass to the database. Remember the perfromance of order by rand() is low. For better understanding see this answer

Community
  • 1
  • 1
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56