[ verodostojan @ 23.01.2014. 21:33 ] @
Pozdrav svima.

Funkcija u klasi nikako da proradi kako treba. Ne izbacuje nikakvu gresku ali jednostavno ne funkcionise.

Funkcija i klasi DataBase_class.php

Code:


public function upit($value)
        {
            $this->__error = FALSE;

                    $sql = "SELECT * FROM korisnici WHERE korisnicko_ime = ".Input::input($value);
                    
                    if ($this->__query = $this->__pdo->query($sql))
                    {
                         $this->__result = $this->__query->fetchAll(PDO::FETCH_OBJ);
                $this->__count = $this->__query->rowCount(); //Ovo ne radi kako treba
               
                    }
                
                else {
                $this->__error = TRUE;
 }   
 return $this;
            }

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



Da ne pisem sad celu klasu, PDO konekcija prema bazi je pravilno definisana ($__pdo property), kao i instanca pomocu koje se komunicira sa bazom ($__instance property).
Isto vazi i za Input klasu

index.php izgleda ovako:

Code:
<?php 

spl_autoload_register(function($class) //Biblioteka koja ucitava sve klase projekta
{
   require_once 'class/'.$class.'.php';
}
    );

$user = DataBase_class::instance()->upit("korisnicko_ime");

if ($user->count())
{
    echo 'Korisnik postoji';
}
else echo 'Korisnik ne postoji';


Uvek kao rezultat daje "Korisnik ne postoji", iako postoji 100%

[Ovu poruku je menjao verodostojan dana 24.01.2014. u 08:28 GMT+1]

[Ovu poruku je menjao verodostojan dana 24.01.2014. u 08:31 GMT+1]
[ Miroslav Ćurčić @ 27.01.2014. 21:42 ] @
Umesto:
$this->__count = $this->__query->rowCount();


probaj:
$this->__count = $this->__result->rowCount();
[ verodostojan @ 28.01.2014. 09:37 ] @
Nije to u pitanju, probao sam odavno, result property je samo niz.
Nedostaju znaci navoda, treba da izgleda ovako:
Code:
$sql = "SELECT * FROM korisnici WHERE korisnicko_ime = '".Input::input($value) . "'";

Mada se u ovakvim situacijama treba koristiti prepare statement umesto query. Dakle:

Code:

 if ($this->__query = $this->__pdo->prepare("SELECT * FROM korisnici WHERE korisnicko_ime = :name"))
                    {
                        $this->__query->bindParam(":name", Input_class::input($value));
                        
                        if ($this->__query->execute())
                        {
                              $this->__result = $this->__query->fetchAll(PDO::FETCH_OBJ);
                                $this->__count = $this->__query->rowCount();
                        }
                    }


Hvala u svakom slucaju.


[ djoka_l @ 28.01.2014. 09:54 ] @
Citat:
PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.


http://www.php.net/manual/en/pdostatement.rowcount.php