venkatpvc Posted August 14, 2015 Share Posted August 14, 2015 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)')); Quote Link to comment https://forums.phpfreaks.com/topic/297774-passing-array-to-pdo-bindparam-giving-me-error/ Share on other sites More sharing options...
requinix Posted August 14, 2015 Share Posted August 14, 2015 The problem is (due to emulated prepares, but is) moot: you cannot use prepared statements there. They are only for passing actual data into a query - you can't use them for things like CREATE TABLEs. Quote Link to comment https://forums.phpfreaks.com/topic/297774-passing-array-to-pdo-bindparam-giving-me-error/#findComment-1518746 Share on other sites More sharing options...
mac_gyver Posted August 14, 2015 Share Posted August 14, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297774-passing-array-to-pdo-bindparam-giving-me-error/#findComment-1518747 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.