?
Solved

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

Posted on 2008-09-29
10
Medium Priority
?
1,658 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:Simon336697
  • 6
  • 4
10 Comments
 
LVL 82

Assisted Solution

by:hielo
hielo earned 2000 total points
ID: 22597634
Instead of VALIDATE_INT try 0:
if(!filter_var($page, 0, 1))
0
 
LVL 82

Expert Comment

by:hielo
ID: 22597644
OR

FILTER_VALIDATE_INT  
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22597736
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:Simon336697
ID: 22597743
The thing is, with my form, i am using POST.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22597777
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
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22597789
>>The thing is, with my form, i am using POST
Try changing ALL the $_POST and all the $_GET to
$_REQUEST
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22597848
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
 
LVL 1

Author Comment

by:Simon336697
ID: 22597869
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
 
LVL 1

Author Comment

by:Simon336697
ID: 22597962
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
 
LVL 82

Expert Comment

by:hielo
ID: 22598003
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question