HELP in array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

HELP in array

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post 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
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Code: Select all

SELECT ID FROM users_logins WHERE userid='$userID' ORDER BY ID DESC LIMIT 1,1;
Last edited by Robert Plank on Fri Jun 30, 2006 12:28 pm, edited 1 time in total.
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post 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
mcoelho123
Forum Commoner
Posts: 37
Joined: Tue Jun 06, 2006 6:27 am

Post 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;
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post 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.
Post Reply