Solved

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

Posted on 2008-09-29
10
1,499 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Get Javascript URL parameters 7 28
How to validate 2 forms at once 8 26
session dropped in IE 10 20
How to fetch your row in php 14 0
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now