Jump to content


Photo

how do I fetch Just the MOST recent Row?


  • Please log in to reply
7 replies to this topic

#1 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 September 2006 - 05:19 PM

I have a small DB (id, groupID, title, MSG , etc)
I have 2 forms of input (via html form = Both work as desired)
one form does edit any field in GroupID
one form does add a new row (can put in any GroupID)
[[[1st question - as of now - I have only 3 different GroupID possibilities
how do I ensure the user does not have a typo and make a new (ie 4th GroupID)
I do no want to use radio or drop down - because I do not want the users to see the 'names' of the other groups]]]

2nd question:
here is the hard part (for me)
I have a small php page that prints a table using the field values in the DB
the ROW it picks is GroupID dependent
it works GREAT // Except when there are TWO (2) rows with the SAME GroupID
so . . . .
how do I just select the row that is MOST recent
(ie had highest id# (which is auto increment)
and yes, if there are 2 rows with the same GroupID
I get 2 tables (only want 1)
The value of 'pick1' is determined by a small html radio form with 3 choices
(one for each GroupID)
I imagine the code to fix this is simple - but I have not deduced it yet
thanks


<?php

include 'loginSTSAlertDB.php';

$like=$_POST['pick1'];
$server = MySQL_connect($host, $username, $password) or die(MySQL_error());
$connection = MySQL_select_db($database, $server) or die (MySQL_error());

$result = MySQL_query("SELECT * FROM Alert Where GroupID like '$like' ") ;
while($row = MySQL_fetch_array( $result )){


 echo "     <table border=1 cellspacing=2 cellpadding=2 height=240>";
 echo "     <tr height=60>";
 echo "     <td colspan=3>" ;
 echo "     <IMG SRC=$row[Logo]>";
 echo "     </td>";
 echo "     </tr>";
 
 echo "     <tr>";
 echo "     <td colspan=3>";
 echo "     $row[Title]";
 echo "     </td>";
 echo "     </tr>";
 
 echo "     <tr>";
 echo "     <td colspan=3>";
 echo "     $row[MSG]"; 
 echo "     </td>";
 echo "     </tr>";
 
 echo "     <tr>";
 echo "     <td>";
 echo "     OK";
 echo "     </td>";
 echo "     <td>";
 echo "     <A HREF=\"http://www.".$row[MsgURL]."\" target=_blank>More</A>";
 echo "     </td>";
 echo "     <td>";
 echo "     Later";
 echo "     </td>";
 
 echo "     </tr>";
 
 echo"</table>";
}


#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 September 2006 - 05:23 PM

$result = MySQL_query("SELECT * FROM Alert Where GroupID like '$like'  order by GroupID DESC") ;

or

$result = MySQL_query("SELECT * FROM Alert Where GroupID like '$like'  order by GroupID ASC") ;

change this

while($row = MySQL_fetch_array( $result )){

to

while($row = MySQL_fetch_assoc( $result )){


if that issnt enough you can also limit the amount of information seen.

limit 1

or 1 - 3231231323 what ever ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 September 2006 - 05:47 PM

well _ tried it
still made 2 tables
then I changed the order by GroupID ascii to
order by id asci (thinking GroupID (which is a text string) does not vary between like rows)

any other guesses?
ps
looked up the string => "$row = MySQL_fetch_assoc"
seems to return a row, not an array?
guess that is good?

also
I do not understand his suggestion ==>

if that issnt enough you can also limit the amount of information seen.

limit 1

or 1 - 3231231323 what ever ok.


thanks



#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 September 2006 - 06:00 PM

... ORDER by id DESC LIMIT 1";

That'll display a single data set with the highest value of id.
Legend has it that reading the manual never killed anyone.
My site

#5 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 September 2006 - 06:11 PM

ok
will try
. . . intuitively - would have guess asc to pick the highest value?
but guess intuition is not always the norm in programming  ::)

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 September 2006 - 06:13 PM

ASC = ascending order, so the first one would be the lowest number.
Legend has it that reading the manual never killed anyone.
My site

#7 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 September 2006 - 06:13 PM

worked !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
:-* :-* :-* :-*

#8 greggustin

greggustin
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 02 September 2006 - 06:17 PM

ASC = ascending order, so the first one would be the lowest number.


understood - guess that means the whole array was 'analyzed' BEFORE it was fetched (ie in the DB))
I would have guess it had to be 'fetched'  before  it was analyzed

no matter - as long as it us understood -thanks again
ps
GREAT forum !!!!!!!!!!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users