jacob1986 Posted December 24, 2015 Share Posted December 24, 2015 I keep running into the error 'Warning PDOStatment::exucute():SQLSTATE[HY093]: invalid parameter number: number of bound variables does not match number of tokens in DB.PHP line 38'. I think it's something to do with the PDO bound variables not being the same, but when I search for an error I cannot seem to find anything (I guess it would help to know what precisely I'm looking for - sorry). I have uploaded the code to a GitHub repository, which can found at https://github.com/aaron1986/OOP-Login-Register-System Quote Link to comment https://forums.phpfreaks.com/topic/299996-register-system-error/ Share on other sites More sharing options...
benanamen Posted December 24, 2015 Share Posted December 24, 2015 I guess it would help to precisely see the query you are trying to run or the page you get the error on. We are not going to dig through your entire project to find your problem. Quote Link to comment https://forums.phpfreaks.com/topic/299996-register-system-error/#findComment-1528573 Share on other sites More sharing options...
jacob1986 Posted December 24, 2015 Author Share Posted December 24, 2015 I thought it would be easier for you, but here's the code below. The error show five times: Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\Work\classes\DB.php on line 37 DB.php:<?phpclass DB {private static $_instance = null;private $_pdo,$_query,$_error = false,$_results,$_count = 0;private function __construct() {try {$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));} catch(PDOException $e) {die($e->getMessage());}}public static function getInstance() {if(!isset(self::$_instance)) {self::$_instance = new DB();}return self::$_instance;}public function query($sql, $params = array()) {$this->_error = false;if($this->_query = $this->_pdo->prepare($sql)) { $x = 1; if(count($params)) {foreach($params as $param) {$this->_query->bindValue($x, $param);$x++;}}if($this->_query->execute()) {$this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);$this->_count = $this->_query->rowCount();}else{$this->_error = true;}}return $this;}public function action($action, $table, $where = array()) {if(count($where) == 3) {$operators = array('=', '>', '<', '>=', '<=');$field = $where[0];$operator = $where[1];$value = $where[2];if(in_array($operator, $operators)) {$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";if(!$this->query($sql, array($value))->error()) {return $this;}}}return false;}public function get($table, $where) {return $this->action('SELECT *', $table, $where);}public function delete($table, $where) {return $this->action('DELETE', $table, $where);}public function insert($table, $fields = array()) {$keys = array_keys($fields);$values = '';$x = 1;foreach($fields as $field) {$values .= '? ';if($x < count($fields)) {$values .= ', ';}$x++;$sql = "INSERT INTO users (`" . implode('`, `', $keys) . "`) VALUES ({$values})";if(!$this->query($sql, $fields)->error()) {return true;}}return false;}public function update($table, $id, $fields) {$set = '';$x = 1;foreach($fields as $name => $value) {$set .= "{$name} = ?";if($x < count($fields)) {$set .= ', ';}$x++;}$sql = "UPDATE {$table} SET {$set} WHERE id = {id}";if(!$this->query($sql, $fields)->error()) {return true;}return false;}public function results() {return $this->_results;}public function error() {return $this->_error;}public function count() {return $this->_count;}} Quote Link to comment https://forums.phpfreaks.com/topic/299996-register-system-error/#findComment-1528574 Share on other sites More sharing options...
mac_gyver Posted December 24, 2015 Share Posted December 24, 2015 lol, that may be where the error is occurring, but to find out what's causing the error you will need to determine what the sql query statement in $sql is and what the parameters are in $params, then backtrack to find the code that's producing those and find and fix why it isn't doing the correct things. i would echo $sql; and print_r($params); inside the ->query(....) method as a start. Quote Link to comment https://forums.phpfreaks.com/topic/299996-register-system-error/#findComment-1528576 Share on other sites More sharing options...
ginerjm Posted December 24, 2015 Share Posted December 24, 2015 Awful lot of functions that don't do much more than the simple line of code could do for you. The message simply means you don't have the same number of arguments bound to the query as you have symbolic parms in .the query statement. As said - echo out the query statement being prepare and then do a print_r on the parms and make sure you have matching counts. Quote Link to comment https://forums.phpfreaks.com/topic/299996-register-system-error/#findComment-1528578 Share on other sites More sharing options...
sn00pers Posted December 25, 2015 Share Posted December 25, 2015 I haven't gotten to really look thoroughly through the code. However at first glance I notice that on line 39, you have hard coded a single question mark to bind a value to. But at the same time you allow for a variable number of values to be bound. This makes me think that any time more than one value, you will get this error. The question mark represents value 1 (provided by $x). if you bind a second value ( #2), there is no '?' in the statement to bind that value to. Again, I may have missed somewhere in the code where this is taken care of, and haven't spent enough time to be completely confident that this is the issue, but thought it worth mentioning. Quote Link to comment https://forums.phpfreaks.com/topic/299996-register-system-error/#findComment-1528585 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.