我必须从我的数据库中获取每一行,其中“level”字段介于mylevel-5和mylevel + 5之间。但我刚接触php和sql并且我不知道如何编写查询...你能帮助我吗?
<?php
session_start();
if(isset($_SESSION['username']))
{
$nickname = $_SESSION['nick'];
}else{
header('location:index.php');
}
include('connection.php');
$sql = mysql_query("SELECT * FROM $tbl_name WHERE nickname = '$nickname'");
$rissql = mysql_fetch_array($sql);
$mylevel = $rissql['Level'];
$sql2 = mysql_query("SELECT * FROM $tbl_name WHERE Level >= $mylevel-5 && Level <= $mylevel+5 order by rand() limit 1");
$rissql2 = mysql_fetch_array($sql2);
$count=mysql_num_rows($rissql2);
if($count > 0)
{
header('location:fightok.php');
}
else
{
header('location:fightno.php');
}
?>
答案 0 :(得分:2)
在编写查询之前尝试设置限制:
$lower = $myLevel - 5;
$upper = $myLevel + 5;
然后使用这些值重新编写您的查询并替换&amp;&amp;使用AND for MySQL:
SELECT * FROM $tbl_name WHERE Level >= $lower AND Level <= $upper ORDER BY rand() LIMIT 1
或使用BETWEEN
SELECT * FROM $tbl_name WHERE Level BETWEEN $lower AND $upper ORDER BY rand() LIMIT 1
谢谢,
安德鲁