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
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);

?>

Link to comment
Share on other sites

Change this

$sql = "select * from comments, users order by id limit $start,$per_page WHERE comments.user_id=users.id";

to this

$sql = "select * from comments, users   WHERE comments.user_id=users.id order by id limit $start,$per_page";

Link to comment
Share on other sites

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.