Jump to content

What im doing wrong? Help plzzzz


princeofpersia

Recommended Posts

Hi guys

 

I have this pagination code and i have no idea what im doing wrong.

 

I have two tables users and comments

 

user table includes

 

id

username

password

email

 

 

comments includes

 

id

title

msg

user_id

 

---------------

 

I am trying to enable users to see their msg by filtering their msg using user_id and id

 

As soon as i add order by id limit $start,$per_page to the query below i get syantax error, if i remove it, it lists all the comments in my table. So really the pagination works fine but as soon as i add limit it gives me

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource................. in line 39

 

thanks for your time

 

 

<?php

session_start();

include ("../global.php");
//welcome messaage
$username=$_SESSION['username'];

$query=mysql_query("SELECT id FROM users WHERE username='$username'");
while($row = mysql_fetch_array($query))
{
$id = $row['id'];

}

$per_page =3; 

if($_GET)
{
$page=$_GET['page'];
}




//get table contents
$start = ($page-1)*$per_page;
$sql = "select * from comments, users order by id limit $start,$per_page WHERE comments.user_id=users.id";
$rsd = mysql_query($sql);

?>


<table width="800px">

	<?php
	//Print the contents

	while($row = mysql_fetch_array($rsd))
	{

		$title=$row['title'];
$msg=$row['msg'];

	?>
	<tr><td style="color:#B2b2b2; padding-left:4px" width="30px"><?php echo $title; ?></td><td><?php echo $msg; ?></td></tr>
	<?php
	} //while
	?>
</table>

Link to comment
https://forums.phpfreaks.com/topic/221772-what-im-doing-wrong-help-plzzzz/
Share on other sites

Try it now.

I just added a part that checks if the page variable is empty or 0

<?php

session_start();

include ("../global.php");
//welcome messaage
$username=$_SESSION['username'];

$query=mysql_query("SELECT id FROM users WHERE username='$username'");
while($row = mysql_fetch_array($query))
{
$id = $row['id'];

}

$per_page =3; 

if($_GET)
{
$page=$_GET['page'];
}




//get table contents
if(($page == '') || ($page == '0'))
{
$start = $per_page;
}
else
{
$start = ($page-1)*$per_page;
}
$sql = "select * from comments, users order by id limit $start,$per_page WHERE comments.user_id=users.id";
$rsd = mysql_query($sql);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.