#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0

    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,396
    Rep Power
    2105
    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 Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0

    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,933
    Rep Power
    4554
    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. --
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0
    Originally Posted by Sepodati
    Why do you only need the last row? Perhaps there's a better way to approach what you're actually trying to solve?
    I already answered this on another thread of mine.
    You login to your twitter or facebook account and see the last post made by your followees.
    You login to my social network and you see the last webpage (last row from the "browsed_url" from the "browsing_history" tbl) that your followee/upline/website visitor/your competition website visitor/etc. visited.
    Look at it this way, you are following Catacaustic, UI Man (me) and Requinix in terms of what webpages we browse. You login to your account and see iframes in the homepage of your account. First iframe shows which webpage Catacaustic viewed latest, second webpage shows which webpage I viewed latest and 3rd iframe shows you what webpage Requinix viewed the latest.
    Difference between my SN and twitter etc. is that with them you view your followees latest posts while with mine you view your followees latest webpages browsed.
    I know that, in order to do things like this I must create different tables for each of your followees where each of their browsed urls get dumped. And so, if I want to do things this way then this query would be important for me.
    But, I have decided few months back to not do things like this. Create different tables for each user as other programmers in other forums warned me that if I do things this way then I would have to create millions of tables and so best I just dump all users' browsed urls (browsing histories) into a single table. Been doing it this way for nearly a yr or over a yr now.
    But nevertheless, there was another reason why I wanted to learn how to pull the last row but I have forgotten what the reason was. nevermind. Best to learn the technique and whenever I need it I can then make use of it.
    Thanks for your concern though Sepodati. You want to help me out and teach me a better way if there is one. Thanks man! i really appreciate it, even though you think I'm the new jerk in php town! I am now more than ever to see you as one of my SN members and see you earn a side income out of it. Nay, make a side living out of it. I want to make you rich even if you already are rich! Same goes for Kicken, Benanamen, Catacaustic, Barand, Dsismad (or whatever his Username is). All code contributors in this forum. But, out of you all I like Kicken better. He never complained or was rude to me. Catacaustic stuck with me for over 6+ months giving me feed-back on my venture ideas. He is always on my mind too. Won't forget him. Kicken was the very first code contributor and the 2nd contributor out of 20 or so forums and so not forgetting him. (The first code contributor was mlukack89 from sitepoint.com forum). Benanamen is one of the first contributors here too. Actually, he first started helping me learn things at codingforums.com (British forum). I know he doesn't like me but still gonna hold onto him. Will make use of his pdo talent when I move to pdo. Very soon.
    Last edited by UniqueIdeaMan; April 15th, 2018 at 11:37 AM.
  10. #6
  11. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,661
    Rep Power
    1822
    Don't forget that it will be first or last depending on the ORDER BY clause being ASCending or DESCending.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    86
    Rep Power
    72
    Why don't you just look through replies to your previous questions for the answer, instead of asking the same ones over and over?

    http://forums.devshed.com/php-develo...ml#post2984297
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0
    Originally Posted by Barand
    Why don't you just look through replies to your previous questions for the answer, instead of asking the same ones over and over?

    http://forums.devshed.com/php-develo...ml#post2984297
    No. This thread and that thread you are referring to are 2 different topics.
    On that thread, I was asking for the latest record/row from each different username.
    On this thread, I was asking for the final record no matter what the user.

    You got your wires crossed somewhere along this line, mate!

    EDIT
    Barand, you missed this post:
    http://forums.devshed.com/php-develo...ml#post2984912
    Last edited by UniqueIdeaMan; April 17th, 2018 at 02:46 PM.
  16. #9
  17. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,396
    Rep Power
    2105
    Originally Posted by UniqueIdeaMan
    No. This thread and that thread you are referring to are 2 different topics.
    On that thread, I was asking for the latest record/row from each different username.
    On this thread, I was asking for the final record no matter what the user.

    You got your wires crossed somewhere along this line, mate!

    EDIT
    Barand, you missed this post:
    http://forums.devshed.com/php-develo...ml#post2984912
    They didn't miss the point. The difference between your two "questions" is literally one short line in the SQL query. If you can get the last one fora certain user, you only need to remove a rally small bit to get the last one for any user.

    That's why you get the greif you do on here. You don't even try to understand the way that the languages work and what they do. You want cut-and-paste answers to everything so you don't have to think for yourself. It doesn't work that way/
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0
    Originally Posted by Catacaustic
    They didn't miss the point. The difference between your two "questions" is literally one short line in the SQL query. If you can get the last one fora certain user, you only need to remove a rally small bit to get the last one for any user.

    That's why you get the greif you do on here. You don't even try to understand the way that the languages work and what they do. You want cut-and-paste answers to everything so you don't have to think for yourself. It doesn't work that way/
    Nah! I understood the query that grabs the last row of the tbl. Sepodati brought it to my sense about the DESC. Used it and it worked.
    Later-on, I opened another thread to get the script to grab all final rows from all Usernames and was stuck on how to achieve this and Barand showed an example which did work. But, when I changed the tbl name and column names then for some reason it started malfunctioning and I need pointing out why and where I am going wrong. How-about you point me to the right direction with a hint ?
    Last edited by UniqueIdeaMan; April 20th, 2018 at 01:53 AM.
  20. #11
  21. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,933
    Rep Power
    4554
    Originally Posted by UniqueIdeaMan
    How-about you point me to the right direction with a hint ?
    ( ͡⚆ ͜ʖ ͡⚆)╭∩╮

    Plenty of hints up there.
    Last edited by Sepodati; April 19th, 2018 at 10:02 PM.
    -- Cigars, whiskey and wild, wild women. --
  22. #12
  23. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,396
    Rep Power
    2105
    Originally Posted by UniqueIdeaMan
    How-about you point me to the right direction with a hint ?
    Take the one that works, and modify it. It's really like remove 2-3 words from a query. Seriously, that's all there is too it. You should know this by now.

    This shows that you're still not putitng any effort into actually learning what to do, and you're just relying on cut-and-paste answers from people and throwing them together until they do something close to what you want them to do. After this long, that's just so wrong.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0
    Originally Posted by Sepodati
    ( ͡⚆ ͜ʖ ͡⚆)╭∩╮

    Plenty of hints up there.
    How on earth did you manage to build that face pic ?
    Mmm. I might aswell build a SN for people like you to build such. A competition perhaps ? Not kidding! I will have this at the back of my head. I think my SN would become demandy. You just made me a rich man another fold!
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    757
    Rep Power
    0
    Originally Posted by Catacaustic
    Take the one that works, and modify it. It's really like remove 2-3 words from a query. Seriously, that's all there is too it. You should know this by now.

    This shows that you're still not putitng any effort into actually learning what to do, and you're just relying on cut-and-paste answers from people and throwing them together until they do something close to what you want them to do. After this long, that's just so wrong.
    I did try but came to a deadend. So, Barand kindly helped me out a 2nd time:
    http://forums.devshed.com/php-develo...ml#post2985059

    Never in a million yrs I would have typed those ".n" as I never came across them and don't know what they mean. So, even if I tried, I would have failed. I only try on those that I am likely to figure things out by myself. Those that I fail to figure-out, I bug the forums. Nowadays, only this one out of 20. I no longer prowl the other 19. Mostly, I used to prowl 10 altogether. The new 10, I did not get much response so quit them within the wk or so. But the former 10 I stuck with for probably a yr. This forum probably 2yrs.
    To prove that I don't just cut & paste and forget without learning anything I will one day show you proof and make you all eat your words.
    Last edited by UniqueIdeaMan; April 30th, 2018 at 11:19 AM.
  28. #15
  29. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,933
    Rep Power
    4554
    One day....
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo