6
class db {

private $_pdo ,
        $_query,
        $_error = false,
        $_results ,
        $_count = 0 ; 

private  function __construct () {
    try {

        $host = config::get('mysql/host');
        $database = config::get('mysql/db');
        $username = config::get('mysql/user');
        $pasword = config::get('mysql/password');

        $this->_pdo = new PDO("mysql:host=$host;dbname=$database", $username, $pasword);

        }  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($tabale , $where) {

    return $this->action('DELETE' ,$table , $where) ;
        }  

public function count() {

    return $this->_count ;
}

 public function error() {

    return $this->_error ;
 }
  } 

index.php

$a = db::getInstance()->get('users',array('username','=','ram')) ;
if(!$a->count()) {

echo "No User" ;
 } else {

echo "OK " ;
  }   

There is an error on index file:

Fatal error: Call to a member function count() on Boolean in line 4.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
gaurav
  • 1,281
  • 1
  • 13
  • 25

2 Answers2

3

Your ->get(..) method returns the value from ->action which is a boolean so do it so:

$a = db::getInstance(); // returns the instance
$a->get('users',array('username','=','ram')); // this return true or false
if(!$a->count()) {
    echo "No User" ;
} else {
    echo "OK " ;
}   

Also you missed some $ at the ->action(), it need to be:

$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?" ;
jmattheis
  • 10,494
  • 11
  • 46
  • 58
  • it give no user output but i set a username ram & codecourse wrote $a = db::getInstance()->get() than compare – gaurav Oct 27 '15 at 07:53
  • @gaurav Try this query in your phpmyadmin `SELECT * FROM users WHERE username = 'ram'` when there are results that the db class has a bug else you do not have the user entry – jmattheis Oct 27 '15 at 08:05
  • in phpmyadmin return entry ram – gaurav Oct 27 '15 at 08:08
  • @gaurav you could also check if `$a->error()` is true – jmattheis Oct 27 '15 at 08:08
  • @gaurav do an ``echo $sql;`` in ``->query(..)`` – jmattheis Oct 27 '15 at 08:16
  • output of echo $sql SELECT * FROM users WHERE {field} {operator} ? – gaurav Oct 27 '15 at 08:18
  • @gaurav you missed the $ ``{$field} {$operator} `` – jmattheis Oct 27 '15 at 08:20
  • @gaurav , Did you get the script to work? Can you post what you did? Thanks – kejo Dec 02 '20 at 02:31
  • @keto i don't know exactly what i did but as jmattheis wrote in answer, i had some mistake in sql query that's why `action` function is returning `false(boolean)` not `$this` & `count` is `db` class function not boolean. `$a` become `false` , I was calling `count` on false. – gaurav Dec 03 '20 at 04:13
0

Misspelling of $table parameter in the delete function. And also the all symbol * is missing from the delete query on the next line.

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47