Jump to content

Archived

This topic is now archived and is closed to further replies.

greggustin

how do I fetch Just the MOST recent Row?

Recommended Posts

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


[code]<?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>";
}[/code]

Share this post


Link to post
Share on other sites
$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.

Share this post


Link to post
Share on other sites
well _ tried it
still made 2 tables
then I changed the [i]order by GroupID ascii[/i] to
[i]order by id asci [/i] (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 ==>

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

limit 1

or 1 - 3231231323 what ever ok.[/quote]

thanks

Share this post


Link to post
Share on other sites
[code] ... ORDER by id DESC LIMIT 1";[/code]

That'll display a single data set with the highest value of id.

Share this post


Link to post
Share on other sites
ok
will try
. . . intuitively - would have guess [b] asc [/b]to pick the highest value?
but guess intuition is not always the norm in programming  ::)

Share this post


Link to post
Share on other sites
ASC = ascending order, so the first one would be the [i]lowest[/i] number.

Share this post


Link to post
Share on other sites
[quote author=AndyB link=topic=106640.msg426659#msg426659 date=1157220789]
ASC = ascending order, so the first one would be the [i]lowest[/i] number.
[/quote]

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

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.