Jump to content

passing array to PDO bindParam giving me error


venkatpvc

Recommended Posts

Friends,

 

I am creating class to create table and  have tried with below script passing array to bindParam. but it's giving me below sql syntax error I really can't figure out what it is. please help on this.

 

public function generate_table($Table_Name, array $Table_Elements) {
        $array_elements = implode(', ', $Table_Elements);
        if ($this->db) {
            $qry = 'CREATE TABLE IF NOT EXISTS ? (?);';
            $stmt = $this->db->prepare($qry);
            $stmt->bindParam(1, $Table_Name, PDO::PARAM_STR);
            $stmt->bindParam(2, $array_elements, PDO::PARAM_STR);
            $stmt->execute();
            if ($stmt->errorInfo()) {
                var_dump($stmt->errorInfo());
            }
            if (!$stmt->errorInfo() && $stmt) { echo "Table Created";}
        }
    }
array(3) { [0]=> string(5) "42000" [1]=> int(1064) [2]=> string(226) "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''STU_INFO' ('STU_ID INT NOT NULL AUTO_INCREMENT, STU_FIRST_NAME VARCHAR(25) NOT ' at line 1" }

I am passing array to script like below

 

$obj = new Create_Table('/home/ubuntu/workspace');
$obj->generate_table('STU_INFO', array ('STU_ID INT NOT NULL AUTO_INCREMENT', 
                                        'STU_FIRST_NAME VARCHAR(25) NOT NULL',
                                        'PRIMARY KEY (STU_ID)'));

 

Link to comment
Share on other sites

the place-holders in prepared queries are for values only (numbers, string data, dates.) they cannot be used to supply identifiers (database, table, column names) or sql syntax.

 

if you are getting any of the information being used to create the table from external input, you will need to validate the information in php code and form and run a non-prepared query. things like database, table, and column names, because they are not used in the query as strings cannot be protected against sql injection by using any string escape functions. if all the information is being using to create the table is produced solely in your code, you would just form an run a non-prepared query.

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.