Locked Posted October 2, 2012 Share Posted October 2, 2012 function reqQuery() { global $datalink; $this->setUid(1); $this->setData('username'); $stmt = $datalink->prepare('SELECT :reqData FROM user_details where id = :reqUid limit 1'); $stmt->bindParam(':reqData',$this->reqData(),PDO::PARAM_STR); $stmt->bindParam(':reqUid',$this->reqUid,PDO::PARAM_INT); echo 'SELECT '.$this->reqData().' FROM user_details where id = '.$this->reqUid().' limit 1'; try { $stmt->execute(); } catch (Exception $e) { $this->err->report_error($e,TRUE); } $stmt->bindColumn('username',$tmp); var_dump($stmt->fetch(PDO::FETCH_OBJ)); } Whats returned from this: SELECT username FROM user_details where id = 1 limit 1 //Echo statment bool(false) // Var Dump But when i change $stmt = $datalink->prepare('SELECT :reqData FROM user_details where id = :reqUid limit 1'); $stmt->bindParam(':reqData',$this->reqData(),PDO::PARAM_STR); $stmt->bindParam(':reqUid',$this->reqUid,PDO::PARAM_INT); To $stmt = $datalink->prepare('SELECT username FROM user_details where id = 1 limit 1'); //$stmt->bindParam(':reqData',$this->reqData(),PDO::PARAM_STR); //$stmt->bindParam(':reqUid',$this->reqUid,PDO::PARAM_INT); I get: SELECT username FROM user_details where id = 1 limit 1 object(stdClass)#8 (1) { ["username"]=> string(9) "Christina" } So am i using bindparam wrong/how should i use it? var_dump($this->reqData()); var_dump($this->reqUid()); returns string( "username" int(1) Quote Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/ Share on other sites More sharing options...
kicken Posted October 2, 2012 Share Posted October 2, 2012 $stmt->bindParam(':reqUid',$this->reqUid,PDO::PARAM_INT); echo 'SELECT '.$this->reqData().' FROM user_details where id = '.$this->reqUid().' limit 1'; You're missing the () after reqUid in your bindParam call. Quote Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/#findComment-1382322 Share on other sites More sharing options...
Jessica Posted October 2, 2012 Share Posted October 2, 2012 I'm pretty sure you can't use bindParam on a column name. Only values. Quote Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/#findComment-1382324 Share on other sites More sharing options...
Locked Posted October 2, 2012 Author Share Posted October 2, 2012 (edited) You're missing the () after reqUid in your bindParam call. Iv was editing/changing it, must have forgot it added it back in I'm pretty sure you can't use bindParam on a column name. Only values. Iv changed it to bindvalue now Still fails but returns object(stdClass)#8 (1) { ["username"]=> string( "username" } (var_dump($stmt->fetch(PDO::FETCH_OBJ)) So its fetching the column now? Updated code global $datalink; $this->setUid(1); $this->setData('username'); $stmt = $datalink->prepare('SELECT :reqData FROM user_details where id = :reqUid limit 1'); $stmt->bindValue(':reqData',$this->reqData(),PDO::PARAM_STR); $stmt->bindValue(':reqUid',$this->reqUid(),PDO::PARAM_INT); echo 'SELECT '.$this->reqData().' FROM user_details where id = '.$this->reqUid().' limit 1'; try { $stmt->execute(); } catch (Exception $e) { $this->err->report_error($e,TRUE); } $stmt->bindColumn('username',$tmp); var_dump($stmt->fetch(PDO::FETCH_OBJ)); Edited October 2, 2012 by Locked Quote Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/#findComment-1382328 Share on other sites More sharing options...
Jessica Posted October 2, 2012 Share Posted October 2, 2012 You're selecting the STRING username from the table. Like "SELECT "foo" from bar" It doesn't matter what else comes after that, because "foo" is a string. Quote Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/#findComment-1382330 Share on other sites More sharing options...
Locked Posted October 2, 2012 Author Share Posted October 2, 2012 You're selecting the STRING username from the table. Like "SELECT "foo" from bar" It doesn't matter what else comes after that, because "foo" is a string. I kind of understand what you mean but not enough that i could fix it... could you point me in the right direction please Quote Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/#findComment-1382331 Share on other sites More sharing options...
kicken Posted October 2, 2012 Share Posted October 2, 2012 You can't bind identifiers (column names, table names, etc). You can only bind values. If you need to use dynamic column/table names you have to use string concatenation to build the query. Quote Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/#findComment-1382342 Share on other sites More sharing options...
Locked Posted October 2, 2012 Author Share Posted October 2, 2012 You can't bind identifiers (column names, table names, etc). You can only bind values. If you need to use dynamic column/table names you have to use string concatenation to build the query. Ah ok i thoguth i could, the silly thing is i have already done that in the reqdata function -.- function reqData() { foreach($this->reqData as $v) { ( $return ? $return = $return.', '.$v : $return = $v); } return $return; } Thats good to know thanks Quote Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/#findComment-1382344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.