Forum Moderators: coopster

Message Too Old, No Replies

Generate itemid from existing DB

         

woldie

11:59 am on Mar 26, 2004 (gmt 0)

10+ Year Member



Hi there,

I've got this simple problem where by I want to create an itemid from a DB and increment it by 1.

Essentially, I've got this query, which gets the first two itemid's in the DB order by date. I need to increment the second itemid by one.

For exmaple:

2283663
2282576

So I want to increment to 2282577.

I'm having trouble with this because its not incrementing itemid by 1, it just displays '1'.

Can anyone help?

See code:

// generate itemid number
$result=mysql_query("select itemid from newsfeed order by prop_date desc limit 2");

while ($row=mysql_fetch_row($result))
{
$itemid_1 = $row[0];
$itemid_2 = $row[1];

echo $itemid_1 . '<br>';
echo $itemid_2;

if ($itemid_2 < $itemid_1)
{
$itemid_2 = $itemid_2 + 1;
echo '<b>'.$itemid_2.'</b>' . '<br>';
}
}

Many Thanks

W.

coopster

12:56 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The issue lies in that you are trying to take the $itemid_2 value from the same row...and there is only one $itemid per row (you have a "two-rwo" result set returned because you specified LIMIT 2 in your query). Try mysql_result() [php.net] instead:

$result=mysql_query("select itemid from newsfeed order by prop_date desc limit 2"); 
$itemid_1 = mysql_result($result, 0);
$itemid_2 = mysql_result($result, 1);
...

woldie

1:40 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



Thanks Coopster, I'll give that a go....