Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: Php/Mysql

  1. #1
    Join Date
    Apr 2007
    Posts
    31

    Exclamation Unanswered: Php/Mysql[Resolved]

    I want to extract last stored numbers from my database. For example: if minim =15 and maxim = 20 i want to push a form or a buton in my test.php page and show last minim and last maxim from my database.Please help.Thanks.
    ex:
    minim maxim
    15 20 -final
    12 15
    11 13 -first
    That is my code who push minim and maxim in table temperature, in field minim and maxim:

    <?php
    mysql_connect("localhost","root","pass") or die(mysql_error());
    mysql_select_db("test");
    if(isset($_POST['minim']) && isset($_POST['maxim'])) {
    $sql = "INSERT INTO temperatura(minim, maxim)
    VALUES( '{$_POST['minim']}', '{$_POST['maxim']}')";
    $result = mysql_query($sql)or die(mysql_error().'<br><br>'.$sql);
    }

    $result = mysql_query("SELECT minim, maxim FROM temperatura") or die(mysql_error());
    while($row = mysql_fetch_assoc($result)) ;
    ?>

    <form action="" method="post">
    <input type=text name="minim">
    <input type=text name="maxim">
    <input type="submit" name="trimite" value="trimite">
    </form>
    Last edited by flobeats; 05-30-07 at 07:25.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you cannot find the "last" row unless you have a column which will be populated with datetime values for when the row was added, so that you can write a query to get the row with the latest datetime added value
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Apr 2007
    Posts
    31
    Thanks, other opinions..?

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    It looks like you are looking for the maximum of the minim and maxim...

    Code:
    SELECT MAX(minim)
              ,MAX(maxim)
    FROM temperatura

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    aschk, have you ever written code to "push" a value onto a stack? and "pop" a value off the top of the stack?

    the item that is at the top of the stack is the last one that was pushed and not popped

    in fact, he said "extract last stored numbers" not "extract maximum stored numbers"

    see post #2
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  6. #6
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    I understand the concept of push/pop yes.
    It just seemed like he was adding constantly incrementing numbers. Turns out he isn't. Obviously temperature can inc/dec (according to whether another record is added). His initial data is misleading. A better set would have been clearer :
    minim maxim
    15 20 -final
    3 9
    27 31
    11 13 -first

    So, your first answer is the correct one I guess. You HAVE to add another field (datetime) to know which one the latest one was.

  7. #7
    Join Date
    Apr 2007
    Posts
    31
    Sorry for misunderstanding, probable is my english.
    This is my modified code. When I push the button "Afiseaza" I want to extract last minim and maxim stored in table "temperatura". I have 2 fields maxim and minim. I think, i need another one field "id" autoincrement and unique. I have that code, something is wrong in bold code, or missing. I mentioned that i saw code php and mysql for the first time in my life two weks ago..
    Thanks for your time.

    <?php
    mysql_connect("localhost","root","pass") or die(mysql_error());
    mysql_select_db("test");
    if(isset($_POST['minim']) && isset($_POST['maxim']))
    {
    $sql = "INSERT INTO temperatura(minim, maxim)
    VALUES( '{$_POST['minim']}', '{$_POST['maxim']}')";
    $result = mysql_query($sql)or die(mysql_error().'<br><br>'.$sql);
    }

    $result = mysql_query("SELECT minim, maxim FROM temperatura") or die(mysql_error());


    if( isset($_POST['bla']) )
    {
    $query = 'SELECT * FROM `temperatura` ORDER BY `id` DESC LIMIT 1 ';
    $result = mysql_query($query);


    }



    ?>

    <form action="" method="post">
    <input type=text name="minim">
    <input type=text name="maxim">
    <input type="submit" name="trimite" value="Trimite">
    </form>

    <form action="test.php" method="POST">
    <input type="hidden" name="bla" />
    <input type="submit" value="Afiseaza" />
    </form>

  8. #8
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Quote Originally Posted by flobeats
    I think, i need another one field "id" autoincrement and unique...

    ...ORDER BY `id` DESC LIMIT 1
    The physical order of data in a database has no meaning.
    1. you should have already have a primary key in your table
    2. Take r937's advice and add a timestamp to each record
    Last edited by gvee; 05-29-07 at 08:17. Reason: Making things bolder and more colourful!
    George
    Home | Blog

  9. #9
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    If i'm honest I can't see why there is anything wrong with what he has done. A better SQL standard way to do this would be :

    Code:
    SELECT minim
          ,maxim
    FROM temperatura t
    JOIN (
      SELECT max(id)
      FROM temperatura
    ) x ON x.id = t.id
    p.s. Does bla have a value?

  10. #10
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I'm not saying what (s)he has done won't work - but it's just re-he-heally bad practice.
    Last edited by gvee; 05-29-07 at 09:34. Reason: Added a Jim Carey style emphasis
    George
    Home | Blog

  11. #11
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    What's the point of vendor specific functions if you're not going to use them now and again
    To be fair this guy has only be doing this for two weeks. I think he's coming along just nicely.

  12. #12
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Quote Originally Posted by aschk
    To be fair this guy has only be doing this for two weeks.
    Even more reason to teach him the correct method
    George
    Home | Blog

  13. #13
    Join Date
    Apr 2007
    Posts
    31
    Quote Originally Posted by aschk
    p.s. Does bla have a value?
    No. it is just a hidden form with the name "bla" to identify the form who send the request. In php code if exist $_POST['bla'], then make the selection. After the selection I have to make somehow to print the result, but i dont know how. I have 2 attachements..Thanks.
    Attached Thumbnails Attached Thumbnails picture.JPG   picture1.JPG  

  14. #14
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    What error ARE you getting?

  15. #15
    Join Date
    Apr 2007
    Posts
    31
    No error, but i think i need a print or echo here :
    if( isset($_POST['bla']) )
    {
    $query = 'SELECT * FROM `temperatura` ORDER BY `id` DESC LIMIT 1 ';
    $result = mysql_query($query);
    HERE PRINT OR ECHO
    }
    ...to print my data, from minim and maxim..

    The code is good for send minim and maxim to the database.
    But when i want to extract last send data from minim and maxim, doesn't work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •