Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

PDO:: Sta nije u redu sa ovim upitom?

[es] :: PHP :: PDO:: Sta nije u redu sa ovim upitom?

[ Pregleda: 2265 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

verodostojan

Član broj: 252511
Poruke: 110
*.dynamic.isp.telekom.rs.



+9 Profil

icon PDO:: Sta nije u redu sa ovim upitom?23.01.2014. u 21:33 - pre 124 meseci
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]
 
Odgovor na temu

Miroslav Ćurčić
ex mVeliki
Novi Sad

Član broj: 19034
Poruke: 1118
*.dynamic.isp.telekom.rs.



+19 Profil

icon Re: PDO:: Sta nije u redu sa ovim upitom?27.01.2014. u 21:42 - pre 124 meseci
Umesto:
$this->__count = $this->__query->rowCount();


probaj:
$this->__count = $this->__result->rowCount();

"The quieter you become, the more you are able to hear."
Blog | PowerCMS
 
Odgovor na temu

verodostojan

Član broj: 252511
Poruke: 110
*.dynamic.isp.telekom.rs.



+9 Profil

icon Re: PDO:: Sta nije u redu sa ovim upitom?28.01.2014. u 09:37 - pre 124 meseci
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.


 
Odgovor na temu

djoka_l
Beograd

Član broj: 56075
Poruke: 3453

Jabber: djoka_l


+1462 Profil

icon Re: PDO:: Sta nije u redu sa ovim upitom?28.01.2014. u 09:54 - pre 124 meseci
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
 
Odgovor na temu

[es] :: PHP :: PDO:: Sta nije u redu sa ovim upitom?

[ Pregleda: 2265 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.