Jump to content

[SOLVED] mysql paging


plodos

Recommended Posts

<?php
include("dbconfig.php");

function title_case($title) {
// deleted
}

$search = mysql_real_escape_string($_POST['search']);
$type = mysql_real_escape_string($_POST['type']);

$page = $_GET["page"]; 
if(empty($page) or !is_numeric($page)){ 
$page = 1;
}

$limit = 25; 

$query1 = mysql_query("select * from person where $type like '%$search%'") or die (mysql_error () );
$rowNumber = mysql_num_rows($query1); 

$pageNumber = ceil($rowNumber / $limit); 
$start = ($page-1)*$limit; 

$data = mysql_query("select * from person where $type like '%$search%' ORDER BY lname ASC LIMIT $start,$limit") or die (mysql_error ()); 
                     
while($info=mysql_fetch_array($data))
{
	if($info['no']=="1")
	{
                     echo title_case($info['lname'])." ".title_case($info['fname']).", ".$info['country']." <br>";
	}
	if($info['no']=="2")
	{
                     echo title_case($info['lname'])." ".title_case($info['fname']).", ".$info['country']."  <br>";
	}
}


if($page > 1){ 
$back = $page-1;
echo "<p align=\"center\"><b><a href=\"search.php?page=$back\"><< Back </a>";

}else{ 

echo "<p align=\"center\"><b> << Back ";
}

echo " | ";

if($page>=$pageNumber){ 

echo "Next >> </b></p>";

}else{ 

$next = $page+1;

echo "<a href=\"search.php?page=$next\">Next >></a></b></p>";

}
?>

 

Im trying to using paging 0-25, 25-50 .......

 

first page is listing all records 0-25, but when I click to next page....program is giving there errors?...

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 'like '%%'' at line 1

 

I used this paging code before, i didnt face any problem....but now this scipts first page is showing the records 0-25, why the second page is giving this error ?

 

Some of the mysql datas are uppercase/lowercase like ERIK, Erik, erik...and im trying the search with lowercase...it can be an error?

 

without paging

$data = mysql_query("select * from person where $type like '%$search%' ORDER BY lname ASC LIMIT $start,$limit") or die (mysql_error ()); 
                     
while($info=mysql_fetch_array($data))
{
	if($info['no']=="1")
	{
                     echo title_case($info['lname'])." ".title_case($info['fname']).", ".$info['country']." <br>";
	}
	if($info['no']=="2")
	{
                     echo title_case($info['lname'])." ".title_case($info['fname']).", ".$info['country']."  <br>";
	}
}

I want to show these records with paging? :S ? :S ?

Link to comment
Share on other sites

I'm pretty sure you're just losing your search value. When you click next you lose your $_POST value submitted on the first list of links so mysql doesn't know what to search for, I'd suggest passing it to the next page in a session or $_GET (preferred if it's not sensitive information). Also, make sure it won't let you do empty searches :P. If you had all errors enabled it would report you're trying to use a $_POST variable that is not set.

Link to comment
Share on other sites

And, you will be doing yourself a BIG favor if you were to create your queries as string variables so you can echo them to the page when they fail. The SQL error that is returned only contains part of the query making it difficult to debug.

 

Example:

$query = "select * from person where $type like '%$search%'";
$result = mysql_query($query) or die (mysql_error ()."<br>Query: $query");

Link to comment
Share on other sites

yes, ım losing my search value

 

Query: select * from person where like '%%'

it is empty

 

if I use like that, just an idea...I dont knpw how to write:)

 

echo "<p align=\"center\"><b><a href=\"search.php?page=$back&search=$search&type=$type\"><< Back </a>";

 

What must I do for work this code ?

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.