Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL
Author Message
micah
Hangin' Around



Joined: May 25, 2006
Posts: 40

PostPosted: Fri Mar 12, 2010 11:25 am Reply with quote

Hey all,

i am trying to select the 2nd from max value from a table. I know that MAX () will select the max but how do I select the 2nd from max?

example of what I am doing

if ($sid == max()) {
include("me.htm");
}

thanks,
Micah
 
View user's profile Send private message
djmaze
Subject Matter Expert



Joined: May 15, 2004
Posts: 727
Location: http://tinyurl.com/5z8dmv

PostPosted: Fri Mar 12, 2010 1:50 pm Reply with quote

Code:
SELECT id FROM table ORDER BY id DESC LIMIT 1 OFFSET 1

_________________
$ mount /dev/spoon /eat/fun auto,overclock 0 1
ERROR: there is no spoon
http://claimedavatar.net/ 
View user's profile Send private message Visit poster's website
micah







PostPosted: Fri Mar 12, 2010 3:08 pm Reply with quote

thanks djmaze for the tip but is there an easier way to pull this variable so that I can include this line in

if ($sid == max()) {
include("me.htm");
}
 
Palbin
Site Admin



Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania

PostPosted: Fri Mar 12, 2010 3:55 pm Reply with quote

Is this in RN / *nuke or just PHP in general?

_________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. 
View user's profile Send private message
micah







PostPosted: Fri Mar 12, 2010 3:58 pm Reply with quote

HI palbin, this is in phpnuke.
 
Raven
Site Admin/Owner



Joined: Aug 27, 2002
Posts: 17088

PostPosted: Fri Mar 12, 2010 10:22 pm Reply with quote

Code:
$sql = 'SELECT id FROM table ORDER BY id DESC LIMIT 1 OFFSET 1';

$row = $db->sql_fetchrow($db->sql_query($sql));
if ($sid == $row['id']) {
  include("me.htm");
}


This assumes that you are using the $db->sql_* syntax and not the antiquated $dbi syntax for data base access Wink
 
View user's profile Send private message
micah







PostPosted: Fri Mar 12, 2010 10:39 pm Reply with quote

Hi Raven,

I kinda see what you did there but part of this may be my problem Smile

instead of using mysql can we not use an if command to pull the 2nd or 3rd from max sid "story id) to include a file between stories the stories in the news module.

If I change max () to a story number the file me.htm prints between the stories like a hot darn.

If I use max () I can print the file to the top article.

I guess my thing is that I can not figure out how to get the file "me.htm" to display below the 2nd story without having to ente teh story number

my origional thought that did not work was


if ($sid == max -2 ()) {
include("me.htm");
}



This displays the "me.htm" article below the 12252 story on the news module
if ($sid == 12252) {
include("me.htm");
}

Thanks very much
 
Raven







PostPosted: Fri Mar 12, 2010 10:58 pm Reply with quote

The SQL that you were given needs you to replace id and table with the actual values that are in your database/table. So, you need to actually write your sql statement like

$sql = 'SELECT sid FROM nuke_stories ORDER BY sid DESC LIMIT 1 OFFSET 1';
or
$sql = 'SELECT sid FROM nuke_stories ORDER BY sid DESC LIMIT 1 OFFSET 2';

When you say second from I'm not sure which value you are actually after but its controlled by the offset.
 
Raven







PostPosted: Fri Mar 12, 2010 10:59 pm Reply with quote

So your code should now look like

Code:
SELECT sid FROM nuke_stories ORDER BY sid DESC LIMIT 1 OFFSET 1

$row = $db->sql_fetchrow($db->sql_query($sql));
if ($sid == $row['sid']) {
  include("me.htm");
}
 
micah







PostPosted: Fri Mar 12, 2010 11:32 pm Reply with quote

HI raven ... I still can't get it as the screen goes white when I place that code.

Can I pay you to do this for me.

Micah
 
Raven







PostPosted: Sat Mar 13, 2010 1:52 am Reply with quote

Sorry! Try this instead.

Code:
$sql = 'SELECT sid FROM nuke_stories ORDER BY sid DESC LIMIT 1 OFFSET 1';

$row = $db->sql_fetchrow($db->sql_query($sql));
if ($sid == $row['sid']) {
  include("me.htm");
}
 
Guardian2003
Site Admin



Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam

PostPosted: Sat Mar 13, 2010 3:30 am Reply with quote

You'll also need somewhere on your page
Code:
global $db;
if you don't already have it.
If your code is inside a function that line should be inside the function too, before the $sql
 
View user's profile Send private message Send e-mail
micah







PostPosted: Sat Mar 13, 2010 8:35 am Reply with quote

awesome ... thanks ... wow

your help was above and beyond once again

Very appreciated
 
Palbin







PostPosted: Sat Mar 13, 2010 8:37 am Reply with quote

Code:


global $db, $prefix;

$sql = 'SELECT sid FROM ' . $prefix . '_stories ORDER BY sid DESC LIMIT 1 OFFSET 1';
$row = $db->sql_fetchrow($db->sql_query($sql));
if ($sid == $row['sid']) {
  include("me.htm");
}


Just wanted to note that you should have the $prefix in there.
 
Raven







PostPosted: Sat Mar 13, 2010 9:22 pm Reply with quote

micah wrote:
awesome ... thanks ... wow

your help was above and beyond once again

Very appreciated

RavensScripts
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©