简体   繁体   English

如何从php mysql中获取的行中获取第一个id?

[英]How to get first id from the fetched rows in php mysql?

I am fetching data in php from mysql table. 我正在从mysql表中获取php中的数据。 Let say I retrieved 15 rows containing 15 unique ids. 假设我检索了包含15个唯一ID的15行。 Now I want to get the the very first fetched id from the results. 现在,我想从结果中获取第一个获取的ID。

This is what I'm trying..but its giving me the last id 这是我正在尝试的..但是它给了我最后一个ID

    <?php

  $results = $db->query("SELECT * FROM orders where Sales_Rep='$sales_rep'");
  while($row = $results->fetch_assoc()){ 
?>
        <tr>
            <td><?php echo $row["Order_ID"] ?></td>
            <td><?php echo $row["Company_Name"]?></td>  
        </tr>
<?php 
        $last_order_date= $row["Order_ID"]; 
        echo $last_order_date;
        } //end of while loop
?>

you could simply add index like this 你可以像这样简单地添加索引

$i = 0;

while loop
    $i++;
    if($i === 1)
        {get ID}
    //rest of your while loop
endwhile;

you can modify your sql to 您可以将SQL修改为

SELECT * FROM orders where Sales_Rep='$sales_rep' Order By Order_ID DESC

or you use this code 或者您使用此代码

    <?php

      $results = $db->query("SELECT * FROM orders where Sales_Rep='$sales_rep'");
$i =0;
$first_id = 0;
      while($row = $results->fetch_assoc()){ 
if ($i===0){
$first_id  = $row["Order_ID"];}
$i++;
    ?>
            <tr>
                <td><?php echo $row["Order_ID"] ?></td>
                <td><?php echo $row["Company_Name"]?></td>  
            </tr>
    <?php 
            $last_order_date= $row["Order_ID"]; 
            echo $last_order_date;
            } //end of while loop
    ?>

your first Id is in $first_id 您的第一个ID在$ first_id中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM