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) 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. 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. 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 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)); 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. 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 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. 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 Link to comment https://forums.phpfreaks.com/topic/269012-pdobindparam-error/#findComment-1382344 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.