2011-12-22 14 views
5

Sto cercando di contare il numero di righe in un risultato e continuo a ricevere l'errore sopra riportato. Ho controllato il manuale e sto usando mysqli_result :: num_rows() come dovrei essere (sto usando lo stile orientato agli oggetti). Ho tre classi che lavorano qui.Chiamata alla funzione non definita mysqli_result :: num_rows()

Class (Connection):

class utils_MysqlImprovedConnection { 
    protected $_connection; 

    public function __construct($host, $user, $pwd, $db) 
    { 
     $this->_connection = @new mysqli($host, $user, $pwd, $db); 
     if(mysqli_connect_errno()) { 
      throw new RuntimeException('Cannot access database:' . mysqli_connect_error()); 
     } 
    } 

    public function getResultSet($sql) 
    { 
     $results = new utils_MysqlImprovedResult($sql, $this->_connection); 
     return $results; 
    } 

    public function __destruct() { 
     $this->_connection; 
    } 
} 

Class (Maniglie Risultato): la funzione

class utils_MysqlImprovedResult implements Iterator, Countable { 
    protected $_key; 
    protected $_current; 
    protected $_valid; 
    protected $_result; 


    public function __construct($sql, $connection) { 
     if (!$this->_result = $connection->query($sql)){ 
      throw new RuntimeException($connection->error . '. The actual query submitted was: '. $sql); 
     } 
    } 

    public function rewind() 
    { 
     if (!is_null($this->_key)){ 
      $this->_result->data_seek(0); 
     } 
     $this->_current = $this->_result->fetch_assoc(); 
     $this->_valid = is_null($this->_current) ? false : true; 
    } 
    public function valid() 
    { 
     return $this->_valid; 
    } 
    public function current() 
    { 
     return $this->_current; 
    } 
    public function key() 
    { 
     return $this->_key; 
    } 
    public function next() 
    { 
     $this->_current = $this->_result->fetch_assoc(); 
     $this->_valid = is_null($this->_current) ? false : true; 
     $this->_key++; 
    } 
    public function count() 
    { 
     $this->_result->store_result(); 
     $this->_result->num_rows(); 
    } 
} 

Classe:

public function resetPassword($email, $pass){ 
    //check if email exists, update authkey and password, send email 
    $sql = "SELECT * FROM table WHERE column = '$email'"; 
    $results = $this->_db->getResultSet($sql); 
    if($results->count() == 1){ 
     // Process 
     $this->_message = "Success!"; 
     return $this->_message; 
    } else { 
     // Not unique 
     $this->_error = "Try again"; 
     return $this->_error; 
    } 
} 

La pagina di prova che sto usando per chiamare tutto questo è (l'istruzione include è solo la funzione __autoload() che funziona correttamente):

$columnvar = '[email protected]'; 
$pass = 'blah'; 
require_once 'inc.init.php'; 
$user = new utils_User(); 
try{ 
    $string = $user->resetPassword($email, $pass); 
    echo $string; 
} 
catch(Exception $e) { 
    echo $e; 
} 
+0

Nota: ho inizialmente non ho avuto il: –

+0

Nota: ho inizialmente non avevo il 'codice' $ this-> store_result (); 'codice' nella funzione, l'ho aggiunto dopo alcune ricerche e ho pensato che potesse essere d'aiuto. –

+0

Rimuovere le parentesi. $ This -> _ result-> numero_colonne(); a $ this -> _ result-> num_rows; Ha funzionato per me –

risposta

6

Dal manuale, sembra che mysqli_result::num_rows non sia una funzione, ma piuttosto una variabile contenente il numero di righe.

Esso può essere utilizzato in questo modo:

$num_rows = $mysqli_result->num_rows; 

La funzione equivalente è mysqli_num_rows($result), dove si passa nell'oggetto mysqli_result, ma questo è se si sta utilizzando lo stile procedurale piuttosto che orientato agli oggetti stile.

Nel codice, si dovrebbe cambiare la vostra funzione count() nella classe utils_MysqlImprovedResult per essere come questo (sto supponendo che è la funzione in cui che stai ricevendo il messaggio di errore),

public function count() 
{ 
    // Any other processing you want 
    // ... 
    return $this->_result->num_rows; 
} 

o, in alternativa, se volete mescolare gli stili OO e procedurali (probabilmente una cattiva idea),

public function count() 
{ 
    // Any other processing you want 
    // ... 
    return mysqli_num_rows($this->_result); 
} 
+0

Non è così che viene usato qui? Ho controllato il manuale, e questo è esattamente come è usato anche nel manuale. –

+0

Nel tuo codice hai "$ this -> _ result-> num_rows();", questo non è il modo in cui è usato nel manuale. Nel manuale è descritto come "int $ mysqli_result-> num_rows;" (notare la mancanza di parentesi di funzione "()"). Cambialo in "return $ this -> _ result-> num_rows;" nel tuo codice e dovrebbe funzionare. –

+0

Grazie ... è stata la mia prima domanda in assoluto qui. Il piacere di fare affari con te. –

Problemi correlati