PHP 从数据库中获取唯一的随机值
我一直在使用 PHP 测验程序,但我在从数据库中获取唯一的随机值时有点迷失。目前这是我的脚本。
<?php $num = 1;
$sql = "SELECT * FROM questions ORDER BY RAND() LIMIT ".$num;
$sql_exec = mysql_query($sql, $connection);
if(isset($_POST['start']) || isset($_SESSION['xy'])){
while($row = mysql_fetch_array($sql_exec)){
if(!in_array($row['qid'], $_SESSION['xy'])){
echo $row['qid']." -".$row['question']."<br />";
if(isset($_POST['num'])){
$_SESSION['xy'][] .= $row['qid'];
}
}else{
// WHAT WILL I PUT HERE
}
}
} ?>
<form action="<?php $_SERVER[PHP_SELF]; ?>" method="post">
<input type="hidden" name="num" value="1" />
<input type="submit" name="start" value="Start" <?php if(isset($_POST['start']) || isset($_SESSION['xy'])) echo "disabled"; ?> />
<input type="submit" name="submit" value="Continue" />
<input type="submit" name="destroy" value="Destroy" />
</form>
我将每一行的 id 放入会话数组中,以便我可以记录之前的所有问题和 我可以比较新问题是否已经使用 in_array 提出。
问题是如果新的获取数据已经在数组中,程序就会停止,因为我仍然没有其他值,
[idea-01] 我正在考虑在 else 上添加一个新的 select 语句,但我知道这是错误的,因为它位于循环内,并且不能保证该值是唯一的。
[想法-02] 使用会话中的行 id 的数组记录,我正在考虑在上面的选择上放置一个 where 条件。
WHERE qid != $_SESSION['xy']
问题是我必须循环此会话以将每个值与语句进行比较。而且题目有20题,以后还可以扩展。
I've been working with PHP quiz program and I'm a bit lost on fetching unique random value from database. Currently here is my script.
<?php $num = 1;
$sql = "SELECT * FROM questions ORDER BY RAND() LIMIT ".$num;
$sql_exec = mysql_query($sql, $connection);
if(isset($_POST['start']) || isset($_SESSION['xy'])){
while($row = mysql_fetch_array($sql_exec)){
if(!in_array($row['qid'], $_SESSION['xy'])){
echo $row['qid']." -".$row['question']."<br />";
if(isset($_POST['num'])){
$_SESSION['xy'][] .= $row['qid'];
}
}else{
// WHAT WILL I PUT HERE
}
}
} ?>
<form action="<?php $_SERVER[PHP_SELF]; ?>" method="post">
<input type="hidden" name="num" value="1" />
<input type="submit" name="start" value="Start" <?php if(isset($_POST['start']) || isset($_SESSION['xy'])) echo "disabled"; ?> />
<input type="submit" name="submit" value="Continue" />
<input type="submit" name="destroy" value="Destroy" />
</form>
I put the id of each row in a session array so that I can records all the previous question and
that I can compare if the new question is already ask using in_array.
The problem is if the new fetch data is already in array, the program stops because I still have no else value,
[idea-01]
I'm thinking of putting a new select statement on else, but I know it's wrong because it's inside a loop and there's no assurance that the value will be unique.
[idea-02]
With the array records of row ids I have in the session, im thinking of putting a where condition on the select above
WHERE qid != $_SESSION['xy']
The problem is I have to loop this session to compare each values to the statement. Also the questions is 20 items and it can be extended in the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在寻找的是比较函数< /a> 的 mysql.
确保 $_SESSION["anwsered_question"] 中仅存储数字值,否则此查询容易受到 MySQL 注入的攻击
另外:不要使用 ORDER BY RAND(),请参阅
I think what you are looking for is the in comparison function of mysql.
Make sure there are only numeric values stored in $_SESSION["anwsered_question"] otherwise this query would be vulnarable to MySQL injections
Also: Don't use ORDER BY RAND(), see http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/ for more info.