$stmt->bindValue icm limit en offset.

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Chris de Vries

Chris de Vries

06/04/2013 19:05:36
Quote Anchor link
Hallo,

Ik zit me al een tijdje blind te staren op het volgende stuk code.
Deze URL gebruik ik:
http://localhost/php/test/json.php?offset=10&limit=20
De tabel mutaties heeft een paar duizend rows maar ik krijg geen resultaten. Als ik limit en offset hard in de query zet dan werkt wel.
Nu heb ik nog niet zo veel ervaring met PHP en ik kan er maar niet achter komen wat ik fout doe.

Chris

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
//require_once("database.php");

$db = new PDO('mysql:host=localhost;dbname=testdb',
        'testdb', 'testdb',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

if( $stmt = $db->prepare ("Select * from mutaties limit :limit offset :offset"))
{

    $stmt->bindValue(":limit",$_GET['limit']);
    $stmt->bindValue(":offset",$_GET['offset']);
    $stmt->execute();

    $return_arr = Array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC )) {
        array_push($return_arr,$row);
    }

    echo json_encode($return_arr);
}

?>
Gewijzigd op 06/04/2013 19:06:59 door Chris de Vries
 
PHP hulp

PHP hulp

28/03/2024 23:26:27
 
Tim S

Tim S

06/04/2013 19:26:04
Quote Anchor link
Probeer het eens met execute(array(
':limit' => $_GET['limit']
));
 
Chris de Vries

Chris de Vries

06/04/2013 19:32:42
Quote Anchor link
Tim Slootweg op 06/04/2013 19:26:04:
Probeer het eens met execute(array(
':limit' => $_GET['limit']
));


Bedoel je op deze manier?
Ik wist niet dat het kon maar ik krijg nog steeds een lege array.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
//require_once("database.php");

$db = new PDO('mysql:host=localhost;dbname=bank',
        'bank', 'i12login',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

if( $stmt = $db->prepare ("Select * from mutaties limit :limit offset :offset"))
{

    //$stmt->bindValue(":limit",$_GET['limit']);
    //$stmt->bindValue(":offset",$_GET['offset']);
    //$stmt->execute();    

    
    $stmt->execute(array(
    ':limit' => $_GET['limit'],
    ':offset' => $_GET['offset']
    ));


    $return_arr = Array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC )) {
        array_push($return_arr,$row);
    }

    echo json_encode($return_arr);
}

?>







Toevoeging op 06/04/2013 19:34:10:

Zo krijg ik keurig 10 resultaten maar ik wil het met parameters gebruikten.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
//require_once("database.php");

$db = new PDO('mysql:host=localhost;dbname=bank',
        'bank', 'i12login',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

if( $stmt = $db->prepare ("Select * from mutaties limit 10 offset 10"))
{

    //$stmt->bindValue(":limit",$_GET['limit']);
    //$stmt->bindValue(":offset",$_GET['offset']);
    //$stmt->execute();    

    
    $stmt->execute();

    $return_arr = Array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC )) {
        array_push($return_arr,$row);
    }

    echo json_encode($return_arr);
}

?>
 
Tim S

Tim S

06/04/2013 19:59:18
Quote Anchor link
Weet je zeker dat er iets in $_GET['limit'] staat?
 
Chris de Vries

Chris de Vries

06/04/2013 20:33:34
Quote Anchor link
Tim Slootweg op 06/04/2013 19:59:18:
Weet je zeker dat er iets in $_GET['limit'] staat?


ja dat weet ik nu wel zeker.

echo ($_GET['limit']);
echo ("<br>");
echo ($_GET['offset']);

Dit geeft 10 en 10

Toevoeging op 06/04/2013 20:36:51:

en ik ben er ook achter gekomen dat het bijvoorbeeld wel werkt met bijvoorbeeld "select * from mutaties where id = :id"
Maar niet met limit of met offset.

Dit werkt dus prima:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
//require_once("database.php");


$db = new PDO('mysql:host=localhost;dbname=bank',
        'bank', 'i12login',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

if( $stmt = $db->prepare ("Select * from mutaties where id = :id"))
{

    $stmt->bindValue(":id",$_GET['id']);
    
    $stmt->execute();

    $return_arr = Array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC )) {
        array_push($return_arr,$row);
    }

    echo json_encode($return_arr);
}

?>







Toevoeging op 06/04/2013 20:57:08:

Op deze manier kan ik wel voorelkaar krijgen wat ik wil maar ik heb me laten vertellen dat het op deze manier niet veilig is ivm SQL injecties ofzo.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
//require_once("database.php");


$db = new PDO('mysql:host=localhost;dbname=bank',
        'bank', 'i12login',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

if( $stmt = $db->prepare ("Select * from mutaties limit ".$_GET['limit'] . " offset " . $_GET['offset']))
{

    //$stmt->bindValue(":o",$_GET['o']);
    //$stmt->bindValue(":l",$_GET['l']);

    $stmt->execute();

    $return_arr = Array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC )) {
        array_push($return_arr,$row);
    }

    echo json_encode($return_arr);
}

?>
Gewijzigd op 06/04/2013 20:58:01 door Chris de Vries
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.