Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQL计算行数_Php_Mysql_Sql - Fatal编程技术网

Php SQL计算行数

Php SQL计算行数,php,mysql,sql,Php,Mysql,Sql,我在做联赛桌足球,有一个球队简介(比如:Team.php?Team=XXX) 在本页中,我想展示团队XXX在排行榜中的位置 <?php $number = 0; $sql = "SELECT * FROM `leaguetable` WHERE `league` = 'leaguename' ORDER BY pts DESC"; $query = mysql_query($sql); while($rs=mysql_fetch_assoc($qu

我在做联赛桌足球,有一个球队简介(比如:Team.php?Team=XXX) 在本页中,我想展示团队XXX在排行榜中的位置

    <?php 
    $number = 0;
    $sql = "SELECT * FROM `leaguetable` WHERE `league` = 'leaguename' ORDER BY pts DESC";
    $query = mysql_query($sql);
    while($rs=mysql_fetch_assoc($query)){
        $number++;
    ?>
<table>
<thead>
<tr>
<th>Position</th>
<th>Team</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<td><?php echo $number; ?></td>
<td><?php echo $rs['team']; ?></td>
<td><?php echo $rs['pts']; ?></td>
</tbody>
</table>
<?php } ?>
页面列表

    <?php 
    $number = 0;
    $sql = "SELECT * FROM `leaguetable` WHERE `league` = 'leaguename' ORDER BY pts DESC";
    $query = mysql_query($sql);
    while($rs=mysql_fetch_assoc($query)){
        $number++;
    ?>
<table>
<thead>
<tr>
<th>Position</th>
<th>Team</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<td><?php echo $number; ?></td>
<td><?php echo $rs['team']; ?></td>
<td><?php echo $rs['pts']; ?></td>
</tbody>
</table>
<?php } ?>
在team.php中,我想显示TeamXXX的位置

<?php
    $getTeam = mysql_fetch_assoc(mysql_query("SELECT * FROM `leaguetable` WHERE `team`='"$_GET['team']"'");
?>
<table>
<thead>
<tr>
<th>Position</th>
<th>Team</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<td>#########</td>
<td><? echo $getTeam['team']; ?></td>
<td><? echo $getTeam['pts']; ?></td>
</tbody>
</table>
查询

select id, team, pts, rnk
from
(
select leag.id, leag.team, leag.pts,
@rnk := if(leag.pts = @lag, @rnk,
           if(@lag := leag.pts, @rnk + 1, @rnk + 1)) as rnk
from leaguetable leag
cross join ( select @rnk := 0, @lag := null ) params
where league = 'FA Cup'
order by leag.pts desc
) rankings
where team = 'Chelsea'
;
example.php

<?php

/**
 * Mysqli initial code
 * 
 * User permissions of database
 * Create, Alter and Index table, Create view, and Select, Insert, Update, Delete table data
 * 
 * @package         PhpFiddle
 * @link            http://phpfiddle.org
 * @since           2012
*/

require_once "dBug!.php";

require "util/public_db_info.php";

$short_connect = new mysqli($host_name, $user_name, $pass_word, $database_name, $port);

if (mysqli_connect_errno())
{
  die("Failed to connect to MySQL: " . mysqli_connect_error());
}

/*
$sql = "create table leaguetable"
     . "("
     . "  id integer primary key not null,"
     . "  team varchar(33) not null,"
     . "  pts integer not null default 0"
     . ");";

$result = $short_connect->query($sql);

if(!$result)
{
    die("Create table failed : " . mysqli_error($short_connect));
}

$sql = "insert into leaguetable"
     . "( id, team, pts )"
     . "values"
     . "( 1, 'Liverpool', 22 ),"
     . "( 2, 'Arsenal', 29 ),"
     . "( 3, 'Chelsea', 23 ),"
     . "( 4, 'Tottenham', 23)";

$result = $short_connect->query($sql);

if(!$result)
{
    die("insert failed : " . mysqli_error($short_connect));
}
*/

//get all tables in the database
//$sql = "SHOW TABLES";

//get column information from a table in the database
//$sql="SELECT COLUMN_KEY, COLUMN_NAME, COLUMN_TYPE FROM information_schema.COLUMNS WHERE TABLE_NAME = 'books'";

//SQL statement for a table in the database
$sql = "select id, team, pts, rnk "
     . "from"
     . "("
     . "select leag.id, leag.team, leag.pts,"
     . "@rnk := if(leag.pts = @lag, @rnk,"
     . "           if(@lag := leag.pts, @rnk + 1, @rnk + 1)) as rnk "
     . "from leaguetable leag "
     . "cross join ( select @rnk := 0, @lag := null ) params "
     . "    where league = 'FA Cup' "
     . "order by leag.pts desc;"
     . ") rankings "
     . where team = 'Chelsea';";

//result is boolean for query other than SELECT, SHOW, DESCRIBE and EXPLAIN
$result = $short_connect->query($sql);

if (($result) && ($result->num_rows > 0))
{
    echo "<table>" . "<thead>" . "<tr>" . "<th>Position</th>" . "<th>Team</th>" . "<th>Points</th>" . "</tr>" . "</thead>" . "<tbody>";
    //convert query result into an associative array
        echo "<tr><td>" . $row['rnk'] . "</td><td>" . $row['team'] . "</td><td>" . $row['pts'] . "</td></tr>";
   echo "</tbody></table>";
}
else
{
    die("select failed : " . mysqli_error($short_connect));
}

$short_connect->close();


?>

如果您不介意使用两个查询(我不介意),这很简单:只需计算有多少队得分更多,然后添加一个(这样,如果三个队并列第二,他们都会获得第二名)

在一个查询中执行此操作有点混乱,因为您需要将此查询嵌入原始查询中:

"SELECT *, (SELECT count(*)+1 FROM leaguetable table2 
                WHERE league = 'leaguename' AND table2.pts > leaguetable.pts) AS POSTN 
     FROM leaguetable WHERE team = '$currentteam'"
但是为什么要使用
mysql.*
API来编写新代码呢?您没有注意到文档中的所有内容吗?帮自己一个忙,从这个程序开始,今天就切换到
mysqli


另外:从不只需将
$\u GET[param]
插入查询字符串中即可!您正在给自己一个SQL注入攻击等待发生。。。在那之前,代码是脆弱的、容易出错的。

希望这会有所帮助

$number = 0;
    $points=0;
        $sql = "SELECT * FROM `leaguetable` WHERE `league` = 'leaguename' ORDER BY pts DESC";
        $query = mysql_query($sql);
        while($rs=mysql_fetch_assoc($query)){
        if($points!=$rs['pts'])
            $number++;

    if($rs['team']==$_GET['team']){
        if($points==$rs['pts'])
            $position=$number-1;
        else
            $position=$number;
    }
    $poins=$rs['pts'];
    }

在mysql中可以通过变量实现这一点,因为团队是按分数排序的(
pts DESC
),而
$number
不给您提供位置吗?取决于您如何定义位置(当两个团队平手时如何),我指的是在Team.php页面中。有位置和如何显示团队的位置[GET VALUE]?team.php?team=xxxx,我想知道xxxx的位置。只有我知道了,非常感谢
$number = 0;
    $points=0;
        $sql = "SELECT * FROM `leaguetable` WHERE `league` = 'leaguename' ORDER BY pts DESC";
        $query = mysql_query($sql);
        while($rs=mysql_fetch_assoc($query)){
        if($points!=$rs['pts'])
            $number++;

    if($rs['team']==$_GET['team']){
        if($points==$rs['pts'])
            $position=$number-1;
        else
            $position=$number;
    }
    $poins=$rs['pts'];
    }