0

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;
    }
12
  • Have you checkd a generated query? A result returned by get? Commented Oct 11, 2015 at 12:07
  • Yes, I've checked it. When I actually print_r the variable $user here, it outputs the correct information from the database, it just does not count it for some reason Commented Oct 11, 2015 at 12:09
  • SO, and what are you going to count in this variable? What's the implementation of count() for user object? Commented Oct 11, 2015 at 12:11
  • why in the last two weeks are there so many SO questions about login scripts? Is university starting off their Computing courses with login scripts? Commented Oct 11, 2015 at 12:16
  • 1
    yes, we need your query method/function of DB class Commented Oct 11, 2015 at 13:13

2 Answers 2

1

check your method :

public function count() {
     return $this->count;
}

versus:

public function query($sql, $params = array()) {
    ...
    $this->_count = $this->_query->rowCount();
    ...
}

I guess you should change count() to:

public function count() {
     return $this->_count;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I missed that typo I guess.
1

From your query method in your class you have this line:

 $this->_count = $this->_query->rowCount();

so when you want to read the count value you can call it with

print $user->_count;

or have you made a getter method to return the _count value? if so you need to show that to us. Also helpful to yourself if you do a var_dump of the $user object to see what values and functions it holds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.