Jump to content

[SOLVED] Getting previous and next record in database


Recommended Posts

Ok I'm using this query for getting the previous ID:

$prev_id = mysql_fetch_array(mysql_query("SELECT * FROM `tracks` WHERE `id` > '$db_id' ORDER by `artist`,`song` ASC LIMIT 1"));

And this to display the link:

if (!empty($prev_id)) {
    $prev = '<a href="index.php?id=music&page=view&song_id='.$prev_id['id'].'" class="boldlink">Previous Song</a>';
} else {
    $prev = '<b>Previous Song</b>';
}

Query for next ID:

$next_id = mysql_fetch_array(mysql_query("SELECT * FROM `tracks` WHERE `id` < '$db_id' ORDER by `artist`,`song` DESC LIMIT 1"));

And this to display the link:

if (!empty($next_id)) {
    $next = '<a href="index.php?id=music&page=view&song_id='.$next_id['id'].'" class="boldlink">Next Song</a>';
} else {
    $next = '<b>Next Song</b>';
}

 

What I'm trying to accomplish is to have links point to the next and previous ID's ordered by artist, then song. Is it possible to do it the way I'm trying? If so, please point me in the right direction. Thanks for all the help so far.

What's $db_id?

 

ASC/DESC is irrelevant if you're limiting to 1 by the way.

 

Also don't SELECT *, it's quicker to SELECT id. SELECT * is returning everything, artist, id, song.. etc... not just id which is what you want.

 

But yeah, what's $db_id?

SELECT `id` FROM `tracks` WHERE 
`id` > '$db_id' 
AND `artist`>=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id')
AND `song`>=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id')
ORDER by `artist`,`song` ASC LIMIT 1

or

$next= '$db_id+1;

$next_id = mysql_fetch_array(mysql_query("SELECT * FROM `tracks` WHERE `id` = '$next' "));

Thanks for the quick replies! I wish it was that simple though, however there are gaps in the id's in my database due to deletions and etc. so that would not work.

SELECT `id` FROM `tracks` WHERE 
`id` > '$db_id' 
AND `artist`>=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id')
AND `song`>=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id')
ORDER by `artist`,`song` ASC LIMIT 1

My code now reads

$prev_id = mysql_fetch_array(mysql_query("SELECT `id` FROM `tracks` WHERE `id` > '$db_id' AND `artist`>=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song`>=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song` ASC LIMIT 1"));
$next_id = mysql_fetch_array(mysql_query("SELECT `id` FROM `tracks` WHERE `id` < '$db_id' AND `artist`<=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id') AND `song`<=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id') ORDER by `artist`,`song` ASC LIMIT 1"));

if (!empty($prev_id)) {
$prev = '<a href="index.php?id=music&page=view&song_id='.$prev_id['id'].'" class="boldlink">Previous Song</a>';
} else {
$prev = '<b>Previous Song</b>';
} if (!empty($next_id)) {
$next = '<a href="index.php?id=music&page=view&song_id='.$next_id['id'].'" class="boldlink">Next Song</a>';
} else {
$next = '<b>Next Song</b>';
}

However, the id's are not being acquired in the right order. Also, some are being skipped.This might be due to the fact that some artists in the database, when ordered by id, are not in alphabetical order perhaps? I'm not sure though as I'm not very advanced with MySQL queries. If you need to see a demo of what's happening, here is the page. You need to click on the play button to view the individual result.

ah ok. you only know the current id so :

 

$dq = mysql_query("SELECT * FROM `tracks` WHERE `id` = '$db_id' ORDER by `artist`,`song` DESC LIMIT 2");

while($db_return = mysql_fetch_row($database_q))

{

$next['artist']=$db_return['artist']';

$next['song']=$db_return['song'];

}

 

$dq = mysql_query("SELECT * FROM `tracks` WHERE `id` = '$db_id' ORDER by `artist`,`song` ASC LIMIT 2");

while($db_return = mysql_fetch_row($database_q))

{

$previous['artist']=$db_return['artist']';

$previous['song']=$db_return['song'];

}

 

I am sure that there is an easier code out there but that code i learned to use.

So after getting the value of the next artist and song, then I query the database like this:

$next_query = 'SELECT `id` FROM `tracks` WHERE `artist`='$next['artist'], `song`='$next['song']';
$prev_query = 'SELECT `id` FROM `tracks` WHERE `artist`='$previous['artist'], `song`='$previous['song']';

Is that correct?

ups try this

SELECT `id` FROM `tracks` WHERE 
`id` > '$db_id' 
AND `artist`=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id')
AND `song`=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id')
ORDER by `artist`,`song`, id ASC LIMIT 1
UNION
SELECT `id` FROM `tracks` WHERE 
`artist` > (SELECT `artist` FROM `tracks` WHERE `id` = '$db_id')
AND `song` > (SELECT `song` FROM `tracks` WHERE `id` = '$db_id')
ORDER by `artist`,`song`, id ASC LIMIT 1

 

yes it seems right to me

I'm now getting this error:" Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING" with this code:

$dq = mysql_query("SELECT * FROM `tracks` WHERE `id` = '$db_id' ORDER by `artist`,`song` DESC LIMIT 2");
while($db_return = mysql_fetch_row($dq)) {
$next['artist'] = $db_return['artist'];
$next['song'] = $db_return['song'];
}	
$dq = mysql_query("SELECT * FROM `tracks` WHERE `id` = '$db_id' ORDER by `artist`,`song` ASC LIMIT 2");
while($db_return = mysql_fetch_row($dq)) {
$prev['artist'] = $db_return['artist'];
$prev['song'] = $db_return['song'];
}
$next_id = mysql_fetch_array(mysql_query("SELECT `id` FROM `tracks` WHERE `artist`='$next['artist']',`song`='$next['song']"));
$prev_id = mysql_fetch_array(mysql_query("SELECT `id` FROM `tracks` WHERE `artist`='$prev['artist']',`song`='$prev['song']"));

ups try this

SELECT `id` FROM `tracks` WHERE 
`id` > '$db_id' 
AND `artist`=(SELECT `artist` FROM `tracks` WHERE `id` = '$db_id')
AND `song`=(SELECT `song` FROM `tracks` WHERE `id` = '$db_id')
ORDER by `artist`,`song`, id ASC LIMIT 1
UNION
SELECT `id` FROM `tracks` WHERE 
`artist` > (SELECT `artist` FROM `tracks` WHERE `id` = '$db_id')
AND `song` > (SELECT `song` FROM `tracks` WHERE `id` = '$db_id')
ORDER by `artist`,`song`, id ASC LIMIT 1

 

I get this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

try to google the problems and find through others posts the solution if you are in hurry

the white_space is the empty gap between the begining of the file and <?php or the end and ?> no empty spaces before the <?php and  ?> - the end

 

(
SELECT * 
FROM  `tracks` 
WHERE  `artist` = ( 
SELECT  `artist` 
FROM  `tracks` 
WHERE  `id` =  '$db_id' ) 
AND song > ( 
SELECT  `song` 
FROM  `tracks` 
WHERE  `id` =  '$db_id' )
)
UNION (

SELECT * 
FROM  `tracks` 
WHERE  `artist` > ( 
SELECT  `artist` 
FROM  `tracks` 
WHERE  `id` =  '$db_id' )
)
ORDER BY  `artist` ,  `song` ASC 
LIMIT 1

Thank you so much! However that code is only working to get the next id. I figured that switching the >'s to <'s would solve the problem, however this just gets the id of the first song when ordered by artist and song. Here is a link to the first record when sorted by artist and song and you can see what I mean.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.