Jump to content

Register System error


jacob1986

Recommended Posts

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 

 

Link to comment
Share on other sites

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:

<?php
class 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;
}

}


 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

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.