Jump to content

Pdo::bindparam Error


Locked

Recommended Posts

 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( 8) "username"

int(1)

Link to comment
https://forums.phpfreaks.com/topic/269012-pdobindparam-error/
Share on other sites

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( 8) "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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.