Solved

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

Posted on 2008-09-29
10
1,521 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 500 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

832 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