Php 从SQL查询中获取结果

Php 从SQL查询中获取结果,php,sql,Php,Sql,我有一个基于船舶信息数据库的MySQL查询,其中包括一个字段ship_name和密钥ship_id 我编写了一个查询,它使用页面的当前船舶id,并根据船舶名称的字母列表查找下一艘船舶 这一切都很好,但是,我正试图在“IF”语句中使用下面的代码创建一个链接 header("Location: shipinfo.php?ship_id=$next_ship_id"); 我不知道如何定义next_ship_id的变量。我尝试了以下行: $next_ship_id = ($ship_id); 理论

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

我编写了一个查询,它使用页面的当前船舶id,并根据船舶名称的字母列表查找下一艘船舶

这一切都很好,但是,我正试图在“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
对于以前的船舶使用:

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 = 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=previous
如果未指定go,则默认情况下将转到上一次装运


不再支持mysql_*函数,它们已经不再维护,将来也将继续使用。您应该使用或更新您的代码,以确保将来项目的功能。

您已经拥有了ship.id,不是吗?不,ship\u id当前基于当前的ship\u id,我想知道如何获取下一个ship\u id,因此,从查询结果中提取ship_id,当我在ship_id=15页面上开始时,它会给我当前:15/下一个:数组,我会在echo Prix中使用它吗?抱歉,对SQLV来说是新的,如此回显电流:$当前船舶id./下一个:$世界其他地区船舶[‘船舶识别号’];或回波电流:$当前船舶id./下一个:$世界其他地区船舶['ship\u information.ship\u id'];我会尽一切努力的,非常感谢你在这个温暖的星期天给予我的帮助。thanks@user2406993好的,我刚刚用上一次和下一次发货的查询样本更新了我的答案,在你尝试之前,确保你得到了最新的更新。hmmm,这就把我带到了最后一艘船sadly@user2406993把它改成min,我用的是max,它会给你最后一个输入错误,请看我更新的上面的查询。它可以继续工作-如果没有一点奇怪的话,我可以直接吻你。