0

I'm using the slim framework to build a restful web service. I have two tables with the following entries:

Table team
id_team    name
1          Brasil
2          England
3          Portugal

Table Person
id_pesorn id_team name     age ...
1          1       Rafinha 23  ...

and the following code:

require '../Slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->response()->header('Content-Type', 'application/json;charset=utf-8');

$app->get('/person/team/:name/:team','getPerson');
$app->run();

function getConn(){
...
}

function getPerson($name, $team){
    $conn = getConn();
    $sql = "SELECT id_team FROM team WHERE name=:team";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $pessoa = $stmt->fetchObject();

    $sql1 = "SELECT * FROM person WHERE id_team=".$pessoa->id_team."and name=:name";
    $stmt1 = $conn->prepare($sql1);
    $stmt1->bindParam("team", $team);
    $stmt1->bindParam("name", $name);
    $stmt1->execute();
    $pessoa1 = $stmt1->fetchObject();

    echo "{pessoas:".json_encode($pessoa1)."}";
}

when I call it by GET: localhost/services/pesorn/team/Rafinha/Brasil gives me a error:

Slim Application Error
Details

Type: ErrorException
Code: 2
Message: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
File: /Applications/XAMPP/xamppfiles/htdocs/aw014/webservice/services/index.php
Line: 38

but should return in JSON:

{"id_person":"1","id_team":"1","nome":"Rafinha","idade":"23",...}

I don't know what I'm doing wrong. Could anyone help me. Thanks.

seal
  • 490
  • 2
  • 5
  • 20
  • Just use `$stmt1->bindParam(":name", $name);`. `$stmt1->bindParam("team", $team);` isn't binding anything so you can remove it. You can see this and more on the doc page: http://www.php.net/manual/en/pdostatement.bindparam.php – Joe May 17 '14 at 15:16
  • Instead of binding param, which you do not do in the first, 'team', query anyway. Use: $stmt->execute(array(':team' => $team)); note the leading ':' on the key. useful link: [12344741/binding-multiple-values-in-pdo](http://stackoverflow.com/questions/12344741/binding-multiple-values-in-pdo) – Ryan Vincent May 17 '14 at 15:21

0 Answers0