使用一个PHP文件运行多个任务
我想做一个每周抽奖系统。这是我想要它做的。每周都会有一个cron作业运行“cron.php”,它将从数据库中随机选择一个用户 一旦这样做了,它将使用JSONAPI在Minecraft服务器上为他们提供一个项目。之后,它会在主网站上发布一条新闻状态,上面写着“格拉茨”之类的话。我可以自己做所有这些事情,但我不知道如何用一个PHP文件来完成这些事情 以下是我到目前为止的情况:使用一个PHP文件运行多个任务,php,mysqli,cron,minecraft,Php,Mysqli,Cron,Minecraft,我想做一个每周抽奖系统。这是我想要它做的。每周都会有一个cron作业运行“cron.php”,它将从数据库中随机选择一个用户 一旦这样做了,它将使用JSONAPI在Minecraft服务器上为他们提供一个项目。之后,它会在主网站上发布一条新闻状态,上面写着“格拉茨”之类的话。我可以自己做所有这些事情,但我不知道如何用一个PHP文件来完成这些事情 以下是我到目前为止的情况: <?php //Selects a random entry from the database and gives
<?php
//Selects a random entry from the database and gives them something in game and also says Congradulations on the website as a news post under the name "TCCraft".
$mysqli = new mysqli("localhost", "uname", "pass", "db with usernames in it");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if ($result = $mysqli->query("SELECT id, name FROM users ORDER BY RAND() LIMIT 1")) { //selects random user from the database.
$row = mysqli_fetch_array($result);
$name = $row['name']; //$name stands for the randomly selected user.
//now its time to run the code to give the player the reward. We are using JSONAPI http://dev.bukkit.org/bukkit-plugins/jsonapi/
require('JSONAPI.php');
$obj = new JSONAPI('IP', 20059, 'unam', 'pass', 535153);
$result = $obj->call("givePlayerItem", array("{$name}, 264, 24"));
$time = date("Y-m-d H:i:s");
//connect to news db
$con = mysqli_connect("localhost", "uname", "pass", "db for all the news posts");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO posts (title, user, body, date) VALUES ('Raffle Winner!','TCCraft','<p>Congradulations to {$name} for winning our weekly raffle!</p><p>If you would like to enter the raffle click <a href='http://tccraft.net/enter.php'>here</a></p>','$time')";
echo "should be added";
}
?>
两个问题:
首先,您只需将INSERT
查询字符串分配给一个变量。您需要执行以下操作:
$mysqli->query($sql);
第二,查询中的引用问题。您需要转义href
标记周围的引号,否则它们将被解释为分隔值
字符串
$sql="INSERT INTO posts (title, user, body, date) VALUES ('Raffle Winner!','TCCraft','<p>Congradulations to {$name} for winning our weekly raffle!</p><p>If you would like to enter the raffle click <a href=\\'http://tccraft.net/enter.php\\'>here</a></p>','$time')";
$sql=“在帖子(标题、用户、正文、日期)中插入值('Raffle Winner!'、'TCCraft'、'p>祝贺{$name}赢得我们的每周抽奖!如果您想输入抽奖,请单击“”、'$time')”;
您只分配了一个包含sql语句的变量,但没有运行它。。。
添加
$res=mysqli\u查询($sql)代码>在$sql=“插入…”之后代码>以什么方式不起作用?您似乎没有检查来自JSONAPI调用的错误。JSONAPI不是问题所在。问题是将新闻添加到数据库中。我应该说我猜。你应该添加错误检查代码。缺失的查询调用是显而易见的,但我很容易就错过了引用问题。您需要检查错误,以便报告语法错误。