Page 1 of 1

HELP in array

Posted: Fri Jun 30, 2006 10:40 am
by mcoelho123
Hi,

I have two sql

Code: Select all

$user_name = $_SESSION['C_Name'];
$userID = $_SESSION['C_UserID'];

$sql2 = "SELECT MAX(ID) AS maxid FROM users_logins WHERE userid='$userID'";
$sql2_query = mysql_query($sql2) or die (mysql_error());
$ID_row = mysql_fetch_assoc($sql2_query);

$sql3 = "SELECT ID FROM users_logins WHERE userid='$userID'";
$sql3_query = mysql_query($sql3) or die (mysql_error());
$ID_num_rows = mysql_num_rows($sql3_query);

if ($ID_num_rows > 1) {
  // $maxID = $ID_row['maxid'] - 1;
  echo $maxID;
} else {
  $maxID = $ID_row['maxid'];
}
I need to select in $maxID the previous number

Obvious i cant do "$maxID = $ID_row['maxid'] - 1" if i have values like 1,2,4,8,... the value selected become the number 7(8-1=7).
How can i do to select the number 4 in this example??

Thanks in advance

Posted: Fri Jun 30, 2006 11:11 am
by RobertGonzalez
mysql_insert_id() may be of some interest to you. Read the entire description as there is good information just before the user comments.

Posted: Fri Jun 30, 2006 11:27 am
by mcoelho123
No i need to use it for another hing not to insert in the database.

[EDITED] And i do not have auto increment activated

Posted: Fri Jun 30, 2006 11:35 am
by Robert Plank

Code: Select all

SELECT ID FROM users_logins WHERE userid='$userID' ORDER BY ID DESC LIMIT 1,1;

Posted: Fri Jun 30, 2006 11:37 am
by mcoelho123
I will simplify :)

Its for the same reason of course but in another way

I have an array:
$row['ID']=1
$row['ID']=2
$row['ID']=3
$row['ID']=8

the max value is 8 and i need to pass the value 3, that is the previous last value to a variable.

Got it :?: :?: :P

Posted: Fri Jun 30, 2006 12:03 pm
by mcoelho123
Ok thanks, it works with a litle change

Code: Select all

SELECT ID FROM users_logins WHERE userid='$userID' ORDER BY ID DESC LIMIT 1,1;

Posted: Fri Jun 30, 2006 12:28 pm
by Robert Plank
mcoelho123 wrote:Ok thanks, it works with a litle change

Code: Select all

SELECT ID FROM users_logins WHERE userid='$userID' ORDER BY ID DESC LIMIT 1,1;
Oh right, fixed, duh what was I thinking.