-1

What is wrong with the following code, because my data is missing, and there are returned records :

     $db_stmt = new PDOStatement();
    $db_stmt = $this->db->prepare("SELECT id_translation AS ID, content AS Label FROM ?");        
    $language = "language_translation_" . $request->request_object->language; 
    $db_stmt->bindParam(1, $language);        
    $db_stmt->execute();        
    while ($obj = $db_stmt->fetchObject()){
        $response->response_list[] = $obj;
        unset($obj);
    }

Edit : I am creating a web service to get some data from a mysql database. I test my service with a rest client. (I don't need to echo !!!)

9
  • Why is unset($obj) inside while?
    – ZZ-bb
    Jul 2, 2012 at 11:10
  • to unset the current obj. commented or not i still don't get my data. Jul 2, 2012 at 11:11
  • Yes, I thought so much. But if you unset it inside a loop, it could be the source of the problem. Try moving it after the while.
    – ZZ-bb
    Jul 2, 2012 at 11:14
  • i commented that and my data is still missing. Jul 2, 2012 at 11:15
  • Where do you echo or do something else with the results? (Or does $response->response_list[] do it automatically?)
    – ZZ-bb
    Jul 2, 2012 at 11:17

1 Answer 1

1

i think you cannot bind a parameter to a table name. please try something like

$db_stmt = $this->db->prepare("SELECT id_translation AS ID, content AS Label FROM `$language`");

Hope this helps.

2
  • Be careful with this kind of code. It provides an access point for SQL Injections. Make sure you properly escape and quote all your identifiers. There is no way built into PDO to do this for you.
    – CLo
    Jul 2, 2012 at 14:26
  • i edited to addslashes($language) and it's all fine. thanks a lot for your answers. Jul 2, 2012 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.