hjpotter92
hjpotter92

Reputation: 80639

GET variable not being accessed

I have a simple 7 lined PHPscript, as shown below:

Code

require( "connect.php" );
$ctg = ($_GET['ctg'])? $_GET['ctg']:'movie';
$offset = ($_GET['off'])? $_GET['off']:30;
$query = "SELECT `msg`, `id`, `nick`, `date` FROM `entries` WHERE `ctg` = ? ORDER BY `id` DESC LIMIT 15 OFFSET ?";
$statemnt = $conn->prepare( $query );
$statemnt->bindParam( 1, $ctg, PDO::PARAM_STR );
$statemnt->bindParam( 2, $offset, PDO::PARAM_INT );
$statemnt->execute();
$results = $statemnt->fetchAll( PDO::FETCH_ASSOC );
echo "$ctg\n$offset\n" . count($results) . "\n";

Problem

The problem I am facing is like this: When I execute the file in console(using php fetch.php), all the echo work great. When I open the page in browser with no $_GET variables, the same output is observed.

The output in browser is the same when I provide it with just the ctg indexed-variable(such as http://localhost/fetch.php?ctg=docu) the output is correct; like this:

docu
30
15

But when I pass the off index-variable(either as fetch.php?off=15 OR fetch.php?ctg=docu&off=15 OR fetch.php?off=15&ctg=docu); The output is always like this:

docu
15
0

Question

The output(and var_dump) both show correct values being passed to both variables, then why is the PDO statement not receiving any results? Why does the count($results) remain 0?

Any help is appreciated.


PS

I also included the following statement:

$statemnt->debugDumpParams();

which dumps this in browser(and console):

SQL: [102] SELECT `msg`, `id`, `nick`, `date` FROM `entries` WHERE `ctg` = ? ORDER BY `id` DESC LIMIT 15 OFFSET ?
Params:  2
Key: Position #0:
paramno=0
name=[0] ""
is_param=1
param_type=2
Key: Position #1:
paramno=1
name=[0] ""
is_param=1
param_type=1

Upvotes: 3

Views: 161

Answers (1)

pankar
pankar

Reputation: 1701

Possibly the $off GET variable is being treated as a string whereas int is expected

Change:

$offset = ($_GET['off'])? $_GET['off']:30; 

to

$offset = ($_GET['off'])?(intval($_GET['off']):30; 

Of course proper validation should exist for all your GETvariables

Upvotes: 1

Related Questions