Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z funkcją wykonującą zapytanie do bazy w klasie
Forum PHP.pl > Forum > PHP > Object-oriented programming
Barton
Witam,

Na początek przedstawie Wam najważniejsze pliki:

sql.class.php:

  1. <?php
  2. class sql {
  3.    
  4.    public $connection;
  5.    public $database;
  6.    public $query;
  7.    public $result = array();
  8.    public $row = array();
  9.    public $queries = 0;
  10.    
  11.    public function error() {
  12.        echo ''.mysql_errno().' : '.mysql_error().'
  13. n';
  14.    }
  15.    
  16.    public function connect($host, $user, $password, $database) {
  17.        $this->connection = mysql_connect($host, $user, $password);
  18.        if($this->connection && $database != &#092;"\") {
  19.            $this->database = mysql_select_db($database, $this->connection);
  20.            if($this->database) {
  21.                return TRUE;
  22.            }
  23.            else {
  24.                $this->error();
  25.                mysql_close($this->connection);
  26.                return FALSE;
  27.            }
  28.        }
  29.        else {
  30.            $this->error();
  31.            return FALSE;
  32.        }
  33.    }
  34.  
  35.        
  36.    public function close() {
  37.        if($this->connection) {
  38.            if($this->result) {
  39.                mysql_free_result($this->result);
  40.            }
  41.            mysql_close($this->connection);
  42.            unset($this->query);
  43.            unset($this->result);
  44.            unset($this->database);
  45.            unset($this->connection);
  46.            return TRUE;
  47.        }
  48.        else  {
  49.            $this->error();
  50.        }
  51.    }
  52.    
  53.    public function query($query) {
  54.        if($query!=&#092;"\" && $this->database) {
  55.            $this->query = $query;
  56.            if($this->result) {
  57.                mysql_free_result($this->result);
  58.            }
  59.            if($this->result = mysql_query($this->query, $this->connection)) {
  60.                $this->queries++;
  61.                return $this->result;
  62.            }
  63.            else {
  64.                $this->error();
  65.            }
  66.        }
  67.        else {
  68.            $this->error();
  69.        }
  70.    }
  71.  
  72.    public function fetchAssoc() {
  73.        if($this->database && $this->result) {
  74.            $this->row = mysql_fetch_assoc($this->result);
  75.            if(is_array($this->row)) {
  76.                return $this->row;
  77.            }
  78.            else {
  79.                $this->error();
  80.            }
  81.        }
  82.        else {
  83.            $this->error();
  84.        }
  85.    }
  86.    
  87.    public function numRows() {
  88.        if($numRows = mysql_num_rows($this->result)) {
  89.            return $numRows;
  90.        }
  91.        else {
  92.            $this->error();
  93.        }
  94.    }
  95.    
  96. }
  97.  
  98. ?>


login.php:

  1. <?php
  2.    @require_once('header.php');
  3.    @require('sql.class.php');
  4.  
  5.    // Connect to MySQL database.
  6.    $sql = new sql;
  7.    $sql->connect($dbhost, $dbusername, $dbpassword, $dbname);    
  8.    // END
  9.    
  10.    function loginUser() {
  11.        $sql = new sql;
  12.        $query = 'SELECT login, pass, act, level FROM users WHERE login=\"'.stripslashes(strip_tags($_POST['login'])).'\";';
  13.        $sql->query($query);
  14.        $ile = $sql->numRows();
  15.        if ($ile == 1) {
  16.                  ...
  17.        }
  18.        else {
  19.            echo 'Nie istnieje użytkownik o takim loginie.';
  20.            $_SESSION['level'] = g;
  21.        }
  22.  
  23.    }
  24.    
  25.    if(isset($_POST['send'])) {
  26.        loginUser();
  27.    }
  28.    
  29.    if($_SESSION['level'] == 'g') {
  30.        // tutaj jest formularz
  31.    }
  32.    include ('footer.php');
  33.    ob_end_flush();
  34. ?>


Problem polega na tym, że $sql->query($query); prawdopodobnie nie zwraca mi wyników. Wnioskuję to po błędzie:

  1. a0 :
  2. n
  3. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/superbry/domains/superbryka.netzip.pl/public_html/fonter/sql.class.php on line 88
  4. 0 :
  5. n


Problem leży w funkcji klasy? Zapytaniu? Sam już nie wiem... Są to moje początki z obiektówką więc proszę o wyrozumiałość. winksmiley.jpg
nevt
  1. <?php
  2. // tą linijkę
  3.  
  4. $query = 'SELECT login, pass, act, level FROM users WHERE login=\"'.stripslashes(strip_tags($_POST['login'])).'\";';
  5.  
  6. // popraw na
  7.  
  8. $login = stripslashes(strip_tags($_POST['login']));
  9. $query = "SELECT `login`, `pass`, `act`, `level` FROM `users` WHERE `login`='$login';";
  10. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.