Using filter_var problem: Warning: filter_var() expects parameter 2 to be long

Hi everyone hope you are all well.
Guys im mucking around with pagination of my php page called showtasks_select_v3.php
What im trying to do is to display records from a mysql table but break it up so that there are only X amount of records seen per page.

Im going to post the "working version" called showtasks_select_v2.php of my script (which doesnt have the pagination code), and the current script (showtasks_select_v3.php) which does have it and causes the problem when using pagination.
The error im getting in showtasks_select_v3.php with the pagination stuff is:

Warning: filter_var() expects parameter 2 to be long, string given in C:\wamp\www\archive\lab\showtasks_select_v3.php on line 37

At my house, that line of code is:
if(!filter_var($page, VALIDATE_INT, 1))
which is line 121 in the code snippet below.

Any help greatly appreciated guys.



========================================= showtasks_select_v2.php 
<head>
<link rel="stylesheet" media="all" type="text/css" href="tasks.css" />
<script src="sorttable.js" type="text/javascript"></script>
 
<?php
include ("connectiondb.php");
$tbl_tasks="currenttasks_ctk";
$tbl_projects="projects_pro";
// The below filters correctly.
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro and name_pro ='mcaffee_antivirus'";   
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro and name_pro = ";   
$grabid = $_POST[projectid];
$sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid'";
 
// $sql2="SELECT DISTINCT name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro";
$sql2="SELECT id_pro,name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro GROUP BY name_pro";
 
$result=mysql_query($sql);
$result2=mysql_query($sql2);
 // $res = SELECT * FROM table WHERE `location` = '".$_GET['form_location']."';  
?>
 
</head>
 
<body>
	
<form name="formcombo" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
           <label for="project" class="formlabels" style="margin-top:2%;">Project:</label>                        
<select name="projectid" id="projectid">
                 
                 <?php 
                        while($row2=mysql_fetch_array($result2)) {
                            if ($_POST[projectid] == $row2['id_pro'])
                                 $sel = "selected";
                            else
                                 $sel = "";
                             echo "<option value=\"".$row2['id_pro']."\" $sel>".$row2['name_pro']."\n";      
                       }
                 ?>
           </select>
 
<input type="submit" name="Submit" value="Insert New Task"><br>
</form>            
          
     
<?php
// print_r($_POST);
// echo $_POST['projectid'];
$grab = $_POST['projectid'];
// echo $grab;
?>
 
 
 
<div id="tablewrap">
	<table class="sortable" style="width:700px;">
 
		<thead>
		<tr>
			<td><b>ID</b></td>
			<td><b>Task Name</b></td>
			<td><b>Project</b></td>
			<td><b>Description</b></td>
		</tr>		
	</thead>
	<tbody>	
		<?php while($rows=mysql_fetch_array($result)){ ?>
		<tr>
			<td><?php echo $rows['id_ctk']; ?></td>
			<td><?php echo $rows['name_ctk']; ?></td>
			<td><?php echo $rows['name_pro']; ?></td>
			<td style="width:400px;"><?php echo $rows['description_ctk']; ?></td>
		</tr> <?php } ?>		
	</tbody>
	<tfoot>
	</tfoot>
	</table>
</div>
 
</body>
 
 
========================================= showtasks_select_v3.php 
<head>
<link rel="stylesheet" media="all" type="text/css" href="tasks.css" />
<script src="sorttable.js" type="text/javascript"></script>
 
<?php
include ("connectiondb.php");
$tbl_tasks="currenttasks_ctk";
$tbl_projects="projects_pro";
// The below filters correctly.
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro and name_pro ='mcaffee_antivirus'";   
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro and name_pro = ";   
$grabid = $_POST[projectid];
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid'";
 
// $sql2="SELECT DISTINCT name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro";
$sql2="SELECT id_pro,name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro GROUP BY name_pro";
 
$result=mysql_query($sql);
$result2=mysql_query($sql2);
 // $res = SELECT * FROM table WHERE `location` = '".$_GET['form_location']."';  
?>
 
</head>
 
<body>
	
	<?php
/*********************
PAGINATION AREA:
**********************/
	
// 1) Fetch our page number from the query string. The URL should look something like: name_meanings.php?page=4
$page = $_GET['page'];	
 
// 2) Define how many records we want to show per page: filter_var(variable, filter, options)
$records_per_page = 5;
if(!filter_var($page, VALIDATE_INT, 1))
  $page=1;   
echo "<h3>Showing results for page $page</h3>";
 
// 3) Calculate the offset value to start counting from in the MySQL query. 
//    To do this, we multiply the page number, minus one, by the number of 
//    records we want to show per page e.g. for page 1 - Our starting record will be (1-1)*30
 
$offset = ($page-1) * $records_per_page;
// $result = mysql_query("SELECT * FROM name_meanings ORDER BY id ASC LIMIT $offset, $records_per_page");
 
$sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid' ORDER BY id_pro ASC LIMIT $offset, $records_per_page";
 
	?>
	
	
	
	
	
<form name="formcombo" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
           <label for="project" class="formlabels" style="margin-top:2%;">Project:</label>                        
<select name="projectid" id="projectid">
                 
                 <?php 
                        while($row2=mysql_fetch_array($result2)) {
                            if ($_POST[projectid] == $row2['id_pro'])
                                 $sel = "selected";
                            else
                                 $sel = "";
                             echo "<option value=\"".$row2['id_pro']."\" $sel>".$row2['name_pro']."\n";      
                       }
                 ?>
           </select>
 
<input type="submit" name="Submit" value="Insert New Task"><br>
</form>            
          
     
<?php
// print_r($_POST);
// echo $_POST['projectid'];
$grab = $_POST['projectid'];
// echo $grab;
?>
 
 
 
<div id="tablewrap">
	<table class="sortable" style="width:700px;">
 
		<thead>
		<tr>
			<td><b>ID</b></td>
			<td><b>Task Name</b></td>
			<td><b>Project</b></td>
			<td><b>Description</b></td>
		</tr>		
	</thead>
	<tbody>	
		<?php while($rows=mysql_fetch_array($result)){ ?>
		<tr>
			<td><?php echo $rows['id_ctk']; ?></td>
			<td><?php echo $rows['name_ctk']; ?></td>
			<td><?php echo $rows['name_pro']; ?></td>
			<td style="width:400px;"><?php echo $rows['description_ctk']; ?></td>
		</tr> <?php } ?>		
	</tbody>
	<tfoot>
	</tfoot>
	</table>
</div>
 
</body>

Open in new window

LVL 1
Simon336697Asked:
Who is Participating?
 
hieloCommented:
>>The thing is, with my form, i am using POST
Try changing ALL the $_POST and all the $_GET to
$_REQUEST
0
 
hieloCommented:
Instead of VALIDATE_INT try 0:
if(!filter_var($page, 0, 1))
0
 
hieloCommented:
OR

FILTER_VALIDATE_INT  
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Simon336697Author Commented:
Hielo, im so sorry....i posted incorrect version of v3...

Here is the right version...the one im working on with all the pagination stuff commented out.



This is the pagination code im trying to add Hielo :>)

$page = $_GET['page'];      
$records_per_page = 5;
if(!filter_var($page, 0, 1))
  $page=1;  
echo "<h3>Showing results for page $page</h3>";
$offset = ($page-1) * $records_per_page;

====================================

This is my current $sql statement which works when the pagination code is commented out.

 $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid'";

=====================================

When I uncomment the pagination code, and change the $sql statement to be:

$sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid' LIMIT $offset, $records_per_page";

This is when it keels over
============================================ v3.php
 
<head>
<link rel="stylesheet" media="all" type="text/css" href="tasks.css" />
<script src="sorttable.js" type="text/javascript"></script>
 
<?php
include ("connectiondb.php");
$tbl_tasks="currenttasks_ctk";
$tbl_projects="projects_pro";
$grabid = $_POST[projectid];
 $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid'";
$sql2="SELECT id_pro,name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro GROUP BY name_pro";
 
$result=mysql_query($sql);
$result2=mysql_query($sql2);
?>
 
</head>
 
<body>
	
	<?php
/*********************
PAGINATION AREA:
**********************/
	
// 1) Fetch our page number from the query string. The URL should look something like: name_meanings.php?page=4
$page = $_GET['page'];	
 
// 2) Define how many records we want to show per page: filter_var(variable, filter, options)
/*
$records_per_page = 5;
if(!filter_var($page, 0, 1))
  $page=1;   
echo "<h3>Showing results for page $page</h3>";
*/
// 3) Calculate the offset value to start counting from in the MySQL query. 
//    To do this, we multiply the page number, minus one, by the number of 
//    records we want to show per page e.g. for page 1 - Our starting record will be (1-1)*30
 
// $offset = ($page-1) * $records_per_page;
// $result = mysql_query("SELECT * FROM name_meanings ORDER BY id ASC LIMIT $offset, $records_per_page");
 
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid' LIMIT $offset, $records_per_page";
 
	?>
 
	
<form name="formcombo" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
           <label for="project" class="formlabels" style="margin-top:2%;">Project:</label>                        
<select name="projectid" id="projectid">
                 
                 <?php 
                        while($row2=mysql_fetch_array($result2)) {
                            if ($_POST[projectid] == $row2['id_pro'])
                                 $sel = "selected";
                            else
                                 $sel = "";
                             echo "<option value=\"".$row2['id_pro']."\" $sel>".$row2['name_pro']."\n";      
                       }
                 ?>
           </select>
 
<input type="submit" name="Submit" value="Insert New Task"><br>
</form>            
          
     
<?php
// print_r($_POST);
// echo $_POST['projectid'];
$grab = $_POST['projectid'];
// echo $grab;
?>
 
 
 
<div id="tablewrap">
	<table class="sortable" style="width:700px;">
 
		<thead>
		<tr>
			<td><b>ID</b></td>
			<td><b>Task Name</b></td>
			<td><b>Project</b></td>
			<td><b>Description</b></td>
		</tr>		
	</thead>
	<tbody>	
		<?php while($rows=mysql_fetch_array($result)){ ?>
		<tr>
			<td><?php echo $rows['id_ctk']; ?></td>
			<td><?php echo $rows['name_ctk']; ?></td>
			<td><?php echo $rows['name_pro']; ?></td>
			<td style="width:400px;"><?php echo $rows['description_ctk']; ?></td>
		</tr> <?php } ?>		
	</tbody>
	<tfoot>
	</tfoot>
	</table>
</div>
 
</body>

Open in new window

0
 
Simon336697Author Commented:
The thing is, with my form, i am using POST.
0
 
Simon336697Author Commented:
The error i get is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\archive\lab\showtasks_select_v3.php on line 97

which is:

<?php while($rows=mysql_fetch_array($result)){ ?>

line 90 iin the code snippet
0
 
Simon336697Author Commented:
Hi bud,

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\archive\lab\showtasks_select_v3.php on line 97

which is:

<?php while($rows=mysql_fetch_array($result)){ ?>


where

$result=mysql_query($sql);

and

 $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid' LIMIT $offset, $records_per_page";
0
 
Simon336697Author Commented:
If I use:

$sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid'";

instead of

$sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid' LIMIT $offset, $records_per_page";

The page loads and displays records no problem...just no pagination now.
0
 
Simon336697Author Commented:
Hielo mate, this is getting messy.

The points are yours.

Sorry about this messy question.

Id like to post one more today if that is okay but on something related to this script, but not pagination.

Thanks for your kind help as always champion.
0
 
hieloCommented:
I'm not sure if you are having problems implementing the pagination OR with querying your db. If the first, then refer to the following:
http://www.tonymarston.net/php-mysql/pagination.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.