Previous and Next button issue.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

inoob
New php-forum User
New php-forum User
Posts: 31
Joined: Tue Nov 06, 2012 12:20 pm

Previous and Next button issue.

Postby inoob » Fri Mar 15, 2013 1:53 pm

I have an issue with a Previous and Next script. Basically 2 links going previous or next based on the ID in the address bar. I'd like to know whats wrong bellow because it is not compiling correctly.

=================================
<?php
$db = @ new mysqli("localhost","username","password");
if (mysqli_connect_errno())
{
echo 'ERROR!<br />'.mysqli_connect_errno()
.' - Not connected : '.mysqli_connect_error().'<br />';
die;
}
else
{
$db_connect = $db->select_db("database");
if (!$db_connect)
{
echo 'Error!';
die;
}
}

$id=$_GET['id'];
$id = $currentid;
$prevquery= "SELECT * FROM articles WHERE fldID < $currentid ORDER BY fldID DESC LIMIT 1";
$prevresult = mysql_query($prevquery) or die(mysql_error());
while($prevrow = mysql_fetch_array($prevresult))
{
$previd = $prevrow['fldID'];
}

$nextquery= "SELECT * FROM articles WHERE fldID > $currentid ORDER BY fldID ASC LIMIT 1";
$nextresult = mysql_query($nextquery) or die(mysql_error());
while($nextrow = mysql_fetch_array($nextresult))
{
$nextid = $nextrow['fldID'];
}
?>
<a href="info.php?id=<?php echo $previd; ?>">Previous</a>

<a href="info.php?id=<?php echo $nextid; ?>">Next</a>
=================================
I am receiving this error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY fldID DESC LIMIT 1'. It doesn't seem like its wrong to me. What do you think?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Previous and Next button issue.

Postby seandisanti » Fri Mar 15, 2013 2:07 pm

looks like

Code: Select all


$id 
$currentid;
 

should be

Code: Select all

$currentid = $id;

inoob
New php-forum User
New php-forum User
Posts: 31
Joined: Tue Nov 06, 2012 12:20 pm

Re: Previous and Next button issue.

Postby inoob » Fri Mar 15, 2013 2:13 pm

Thanks for your reply. It works. I find it confusing though. What is the difference between switching both variables on either side when they both equal each other?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Previous and Next button issue.

Postby seandisanti » Fri Mar 15, 2013 2:19 pm

a single equals sign in PHP is an assignment operator, it says that the container on the left should be filled with the value on the right.

so when you say

Code: Select all

$id=$_GET['id'];

you are saying $id gets the value contained in the id field of the $_GET superglobal. following it up with

Code: Select all

$id = $currentid;

says "nevermind, go ahead and replace what we just stored in $id with what's in $currentid (which is nothing).
assignment is always right to left. Just like when you're doing algebra and you're isolating a variable; you may start with
10=4x + 2

in proper form it ends up being
x = 2

because you're assigning the expression on the right to the variable on the left

inoob
New php-forum User
New php-forum User
Posts: 31
Joined: Tue Nov 06, 2012 12:20 pm

Re: Previous and Next button issue.

Postby inoob » Fri Mar 15, 2013 2:26 pm

I'm going to have to revise this. That makes sense. Thank you very much.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Previous and Next button issue.

Postby seandisanti » Fri Mar 15, 2013 2:58 pm

no problem, I'm glad i could help.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest