#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    69
    Rep Power
    1

    Question How To Force MySql With Php To Show Final Row Only?


    Buds!

    How to code so php forces mysql to show only the final row of the column ?
    The followings are how I coded (2 copied youtube tuts) to show all rows to allow user to delete multiple rows:

    SAMPLE 1
    PHP Code:
    <?php
    session_start
    ();
    require 
    "conn.php";
    require 
    "site_details.php"?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Follow Users</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <form name="form" action="" method="post">
    <table border=1 cellpadding=1 cellspacing=1>
        <tr>
            <th>Id</th>
            <th>Username</th>
            <th>Password</th>
            <th>Email</th>
            <th>Delete</th>
        </tr>
    <?php
    $res
    =mysqli_query($conn,"SELECT * FROM users");
    while(
    $row=mysqli_fetch_array($res))
    {
        echo 
    "<tr>";
        echo 
    "<td>"?> <input type="checkbox" name="num[]" class="other" value="<?php echo $row["id"]; ?>" /> <?php echo "</td>";
        echo 
    "<td>"; echo $row["ids"]; echo "</td>";
        echo 
    "<td>"; echo $row["usernames"]; echo "</td>";
        echo 
    "<td>"; echo $row["passwords"]; echo "</td>";
        echo 
    "<td>"; echo $row["emails"]; echo "</td>";
        echo 
    "</tr>";
    }
    ?>
    </table>
    <input type="submit" name="submit" value="delete selected">
    </form>
    <?php
    if(isset($_POST["submit"]))
    {
       
    $box=$_POST['num'];
       while (list (
    $key,$val) = @each ($box))
        {
          
    mysqli_query($conn,"DELETE FROM users WHERE id='$val'");
        }
    ?>
           <script type="text/javascript">
           window.location.href=window.location.href;
           </script>
    <?php
    }
    ?>

    </body>
    </html>
    SAMPLE 2:

    PHP Code:

    <?php
    session_start
    ();
    require 
    "conn.php";
    require 
    "site_details.php"?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Follow Users</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <table border=1 cellpadding=1 cellspacing=1>
        <tr>
            <th>Id</th>
            <th>Username</th>
            <th>Password</th>
            <th>Email</th>
            <th>Delete</th>
        </tr>
    <?php 
    $sql 
    "SELECT * FROM users";
    $result mysqli_query($conn,$sql);
    while(
    $row mysqli_fetch_array($result))
        {
            echo 
    "<tr>";
            echo 
    "<td>".$row['ids']."</td>";
            echo 
    "<td>".$row['usernames']."</td>";
            echo 
    "<td>".$row['passwords']."</td>";
            echo 
    "<td>".$row['emails']."</td>";
            echo 
    "<td><a href=delete2b.php?id=".$row['ids'].">Delete</a></td>";
        }

    ?>
    </table>
    </body>
    </html>
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,264
    Rep Power
    2039
    Basically all that you need to do is set the SQL query to only return the last record. That way you'll only ever need to use that one single record.

    Something like this (keep in mind that I don't know how you're ordering the records so you'll have to sort that our yourself):

    Code:
    SELECT * FROM users ORDER BY usernames DESC LIMIT 1

    Comments on this post

    • UniqueIdeaMan agrees : Good suggestion.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    69
    Rep Power
    1

    Thumbs up


    Originally Posted by Catacaustic
    Basically all that you need to do is set the SQL query to only return the last record. That way you'll only ever need to use that one single record.

    Something like this (keep in mind that I don't know how you're ordering the records so you'll have to sort that our yourself):

    Code:
    SELECT * FROM users ORDER BY usernames DESC LIMIT 1
    Thanks. Another suggested this also.
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,652
    Rep Power
    4495
    Why do you only need the last row? Perhaps there's a better way to approach what you're actually trying to solve?
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo