-2

im working on an hangman game but now i want to change the wordlist. Now the words are stored in a file but I want it in a mysql database.

function findword(){
if(!@$file = file('wrdslst.txt')){
    echo "<div align='center'>";
    echo 'There are no words in the file';
    echo "</div>";  
    exit;
}else{
    $count = count($file)-1;
    if($count == 1){
        return(substr($file[0],0,strlen($file[$r])-2));
    }else{
        function check($word){
            if($word == ''){
                return(false);                  
            }else{
                return($word);
            }               
        }
        $word = false;
        while($word === false){
            $r = rand(0,$count);
            $word = substr($file[$r],0,strlen($file[$r])-2);
            $word = check($word);
        }
        return($word);
    }
}

}

I have the connection setup

include ("../inc/sql_connect.inc.php");
$query = "SELECT word FROM dba_tblwords";
$select = @mysql_query($query);

while($row = mysql_fetch_array(($select)))
{
 $words[] = ($row['word']);
  • 4
    This seems to be incomplete. What exactly is your problem? – Gábor Bakos Apr 27 '15 at 14:33
  • 3
    This looks like a homework question – hardillb Apr 27 '15 at 14:36
  • 5
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 27 '15 at 14:36

1 Answers1

0

First Use mysqli functions, mysql_* functions are no longer used and will trow some errors, you can also use PDO.

MySQLi example

$con = new mysqli($host, $user, $password, $database);
if ($con->connect_error) {
    die('Connect Error (' . $con->connect_errno . ') '
            . $con->connect_error);
}
$query = $con->prepare('SELECT word FROM dba_tblwords');
$result = $query->execute();
while($row = mysqli_fetch_array($result)){
    $words[] = $row['word'];
}

PDO Example

try{
    $pdo = new PDO ('mysql:host=' . $host . ';dbname=' . $database, $username, $password);
} catch (PDOException $ex) {
    die('Could not connect with the database');
}
$query = $pdo->prepare('SELECT word FROM dba_tblwords');
$result = $query->execute();
while($row = $result->fetch()){
    $words[] = $row['word'];
}
N. Hamelink
  • 603
  • 5
  • 14