下面的代码显示了我连接到我的 MYSQL 数据库的连接,但由于某种原因,一旦它从数据库中获取详细信息,之后它就不会更新 PHP 页面。例如,当前表 lastWinner 是一个包含“David”的字符串数据类型。保存 php 文件后,输出显示为 David,但如果我更改 mysql 数据库中的值,则输出不会更改。
<div id="navigation">
<?php
$host = "localhost";
$username = "DB_USER";
$password = "PASSWORD";
$db_name = "DB_NAME";
$message = "The last person to win the lottery is: ";
mysql_connect("$host", "$username", "$password") or die (mysql_error ());
mysql_select_db("$db_name") or die(mysql_error());
$total = "SELECT lastWinner AS total FROM info";
$rs = mysql_query($total);
while($row = mysql_fetch_array($rs)) {
echo $message.$row['total'];
}
mysql_close();
?>
</div>
这是将数据发送到数据库本身的代码。
public static boolean updateInfo() {
try {
if (Settings.DisableMYSQL == true)
return false;
Statement stmt = connection.createStatement();
if (Lottery.getCurrentLotteryWinner() == null
&& Lottery.getLastLotteryWinner() == null)
stmt.executeUpdate("UPDATE info SET lastWinner = 'There current isn't a lottery winner!'");
else if (Lottery.getCurrentLotteryWinner() != null
&& Lottery.getLastLotteryWinner() == null)
stmt.executeUpdate("UPDATE info SET lastWinner = '"
+ Lottery.getCurrentLotteryWinner() + "'");
else
stmt.executeUpdate("UPDATE info SET lastWinner = '"
+ Lottery.getLastLotteryWinner() + "'");
stmt.executeUpdate("UPDATE info SET moneyEarned = '"
+ Lottery.options.size() + "'");
} catch (Throwable e) {
if (System.currentTimeMillis() - lastConnection > 10000) {
destroyConnection();
createConnection();
lastConnection = System.currentTimeMillis();
}
}
return false;
}