我根据船舶信息数据库获得了MySQL查询,其中包括字段ship_name
和密钥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
}
答案 0 :(得分: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";
在你的网址上,下一艘船就是这样的:
http://mysite.com/ships.php?current_ship_id=15&go=next
和上一个一样:
http://mysite.com/ships.php?current_ship_id=15&go=previous
如果未指定go,则默认情况下将转到上一个货船。
mysql_*
个功能不再支持,它们为officially deprecated,不再维护,将为removed个未来。您应该使用PDO或MySQLi更新代码,以确保将来的项目功能。