[PHP] Pagination results from mysql

My now code:
HTML:

 <form action="bans.php?screen=search" method="post">
								<select name="typ">
									<option>first</option>
									<option>second</option>
								</select>
								<input name="search_type" type="input" maxlength="20" />
								<input type="submit" value="search!" name="spr"><br />
							</form>

AND PHP:

if($_POST || $_GET) 
			$submit = isset($_GET['spr']) ? $_GET['spr'] : $_POST['spr'];
			if($submit) {
				if(empty($submit)) {
					header("Location: bans.php?action=search");	
					exit();
				}											
			}
			$method = isset($_GET['typ']) ? $_GET['typ'] : $_POST['typ'];
			$bantype = isset($_GET['search_type']) ? $_GET['search_type'] : $_POST['search_type'];

			$result = mysql_query("SELECT COUNT(`" . $method . "`) AS `ilosc` FROM `" .   $config['DB_PREFIX'] . "bans` WHERE  `" . mysql_real_escape_string($method) . "`  LIKE '%" . mysql_real_escape_string($bantype) . "%'
    ;");
			$ile = mysql_fetch_assoc($result);
			$pages = ceil($ile['ilosc']/10);
			$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
			if($page > $pages || $page < 1){
				$page = 1;

			}

If i’m on first page, displayed good, but I’m on other page this results not displayed.

In first page running form, but on the other does not work anymore…

first page = $_POST, other page = $_GET?

How this do?

My code in first page displayed good, but when i’m on other page returns errors:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/myfile… on line 60

First try the following with $_POST to see what works then repeat with $_GET and spot the difference:

$sql = "SELECT 
COUNT(`" . $method . "`) AS `ilosc` 
FROM `" .   $config['DB_PREFIX'] . "bans` 
WHERE  `" . mysql_real_escape_string($method) . "`  
LIKE '%" . mysql_real_escape_string($bantype) . "%'
    ;";

echo $sql ; 

$result = mysql_query( $sql );

// optional
// echo '<pre>'; var_dump( $result );  echo '</pre>'; // pre formats output

die;

Did you search for “PHP Manual mysql_query()” to check the result:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

Also please note the following:

Warning:
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_query()
PDO::query()

How do you make pagination?
I don’t see LIMIT statement in your SQL query.

Btw, if you don’t care about where is incoming value from ($_GET or $_POST) use $_REQUEST as it holds values from both.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.