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
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));

Edited by Locked
Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.