0

我有一个基于船舶信息数据库的 MySQL 查询,其中包括一个字段ship_name和 key ship_id

我编写了一个使用current_ship_id页面的查询,并根据 的字母列表查找下一艘船ship_names

这一切都很好,但是,我正在尝试使用“IF”语句中的以下代码创建一个链接。

header("Location: shipinfo.php?ship_id=$next_ship_id"); 

我不知道该怎么做是定义变量next_ship_id。我试过这条线:

$next_ship_id = ($ship_id);

理论上,我想得到查询的结果$sql(我知道只有一个结果)并找到它是ship_id.

请问我该怎么做?

$sql = "    SELECT ship_infomation.ship_id
              FROM   ship_infomation
        INNER JOIN (
                  SELECT ship_name 
                    FROM   ship_infomation
                   WHERE  ship_id = $current_ship_id
                   ) As current_ship
                ON ship_infomation.ship_name < current_ship.ship_name
          ORDER BY ship_infomation.ship_name ASC
             LIMIT 1";

// echo "<br /><br />$sql<br /><br />";
$ships = mysql_query($sql, $ships) or die(mysql_error());
$row_ships = mysql_fetch_assoc($ships);
$totalRows_ships = mysql_num_rows($ships);
$next_ship_id = ($ship_id);
if ($totalRows_ships = 1)
{
    header("Location: shipinfo.php?ship_id=$next_ship_id");
}
else
{
    // remain on current page  
}
4

1 回答 1

0

下一艘船使用:

SELECT ship_id 
  FROM ship_infomation 
 WHERE ship_id = (SELECT min(ship_id) 
                    FROM ship_infomation 
                   WHERE ship_id > $current_ship_id) 
 LIMIT 1

对于以前的船舶使用:

SELECT ship_id 
  FROM ship_infomation 
 WHERE ship_id = (SELECT max(ship_id) 
                    FROM ship_infomation 
                   WHERE ship_id < $current_ship_id) 
 LIMIT 1

在你的代码中改变这个:

$ships = mysql_query($sql, $ships) or die(mysql_error());
$row_ships = mysql_fetch_assoc($ships);
$totalRows_ships = mysql_num_rows($ships);
$next_ship_id = ($ship_id);
if ($totalRows_ships = 1)
{
    header("Location: shipinfo.php?ship_id=$next_ship_id");
}
else
{
    // remain on current page  
}

对此:

$ships = mysql_query($sql, $ships) or die(mysql_error());
$row = mysql_fetch_assoc($ships);
$totalRows_ships = mysql_num_rows($ships);
if ($totalRows_ships = 1)
{
    header("Location: shipinfo.php?ship_id=" . $row['ship_id']);
}
else
{
    // remain on current page  
}

对于下一个和上一个,在您的代码上:

$go = isset($_GET['go']) ? $_GET['go'] : 'next';
if ($go == 'next')
    $sql = "SELECT ship_id FROM ship_infomation WHERE ship_id = (SELECT min(ship_id) FROM ship_infomation WHERE ship_id > ". mysql_real_escape_string($current_ship_id) . ") LIMIT 1";
else
    $sql = "SELECT ship_id FROM ship_infomation WHERE ship_id = (SELECT max(ship_id) FROM ship_infomation WHERE ship_id < ". mysql_real_escape_string($current_ship_id) . ") LIMIT 1";

在你的 URL 上,下一艘船是这样的:

http://mysite.com/ships.php?current_ship_id=15&go=next

就像以前一样:

http://mysite.com/ships.php?current_ship_id=15&go=previous

如果没有指定去,默认情况下会去上一艘船。


不再支持功能mysql_*,它们已正式弃用不再维护,将来将被删除您应该使用PDOMySQLi更新您的代码,以确保您的项目在未来的功能。

于 2013-07-14T11:57:16.573 回答