I'm having a problem with my login script. I'm trying to make a method to make my mySQL query handling easier.
My code should return "OK!" to the page, but instead it returns "No user", which means that it counts 0 rows. I've tested the mySQL query on phpMyAdmin, and it works fine there.
My Code:
DB.php:
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;
}
}
}
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
}
public function count() {
return $this->count;
}
Index.php:
$user = DB::getInstance()->get('digi_users.users', array('username', '=', 'chris'));
if(!$user->count()) {
echo 'No user';
} else {
echo 'OK!';
}
Any help is much appreciated!
UPDATE
I'm establishing a database connection in my DB Class (DB.php).
I actually have multiple databases, so I created a couple connections.
Code in DB Class:
private function __construct() {
/* Connect to DB 1 */
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db1'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
/* Connect to DB 2 */
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db2'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
/* Connect to DB 3 */
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db3'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
/* Connect to DB 4 */
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db4'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
/* Connect to DB 5 */
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db5'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
/* Connect to DB 6 */
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db6'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
}
The getInstance() function creates a new DB instance.
Code from DB Class:
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
Query() method from DB Class:
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;
}
get?print_rthe variable$userhere, it outputs the correct information from the database, it just does not count it for some reasoncount()for user object?querymethod/function ofDBclass