PHP/MYSQL 连接刽子手

PHP/MYSQL connect hangman

我正在制作刽子手游戏,但现在我想更改单词表。 现在单词存储在一个文件中,但我希望它存储在 mysql 数据库中。

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);
    }
}

}

我有连接设置

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']);

第一个 使用mysqli函数,mysql_*函数不再使用会报错,也可以使用PDO。

MySQLi 示例

$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 示例

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'];
}