[英]Get result from SQL Query
我有一个基于船只信息数据库的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
}
对于下一艘船,请使用:
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_*
函数,它们已被正式弃用 , 不再维护 ,以后将被删除 。 您应该使用PDO或MySQLi更新代码,以确保将来项目的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.