PHP PDO Cant Bind Params in Class -
this question has answer here:
i cant code bind params mysql query, doing wrong here?
error
fatal error: uncaught exception 'pdoexception' message 'sqlstate[hy093]: invalid parameter number: number of bound variables not match number of tokens' in /users/warren/sites/frame work/modules/user login/classes/db.php:34 stack trace: #0 /users/warren/sites/frame work/modules/user login/classes/db.php(34): pdostatement->execute() #1 /users/warren/sites/frame work/modules/user login/classes/db.php(48): dbconnect->get_contents() #2 {main} thrown in /users/warren/sites/frame work/modules/user login/classes/db.php on line 34
code
<?php include '../../../../config.php'; $dbuser = $config['username']; $dbpass = $config['password']; class dbconnect { public function connect($dbuser,$dbpass) { $this->dbuser = $dbuser; $this->dbpass = $dbpass; try { $this->connect = new pdo('mysql:hostname=localhost;dbname=totalrisk', $this->dbuser, $this->dbpass); $this->connect->setattribute(pdo::attr_errmode, pdo::errmode_exception); return $this->connect; } catch (exception $e) { echo 'error: ' . $e->getmessage(); } } public function select_table($table) { $this->select_table = $table; echo $this->select_table; } public function select_col($col) { $this->select_col = $col; echo $this->select_col; } public function get_contents() { $stmt = $this->connect->prepare('select * :table'); $stmt->bindparam(':table', $this->table , pdo::param_str); $stmt->execute(); $result = $stmt->fetchall(); while($row = $stmt->fetch()) { print_r($row); } } } $conn = new dbconnect(); $conn->connect($dbuser,$dbpass); $conn->select_table('users'); $conn->select_col('site_name'); $conn->get_contents(); ?>
tables must not binded, use this:
$this->connect->prepare('select * ' . $this->select_table);
also notice use table should use select_table
Comments
Post a Comment