2011-12-22 49 views
5

Sonuçtaki satır sayısını saymaya çalışıyorum ve yukarıda verilen hatayı alıyorum. Elkitabını kontrol ettim ve olması gerektiği gibi mysqli_result :: num_rows() kullanıyorum (nesne tabanlı stil kullanıyorum.) Burada üç sınıf çalışıyorum.Tanımlanmamış işlev çağrısı mysqli_result :: num_rows()

Sınıf (Bağlantı):

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; 
    } 
} 

Sınıf (Sonucu kolları):

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(); 
    } 
} 

Sınıf işlevi:

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; 
    } 
} 

Bütün bu çağırmak için kullanıyorum test sayfası (deyim sadece iyi çalışıyorsa __autoload() işlevini içerir):

$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

not karıştırmak istiyorum: –

+0

not: başlangıçta yoktu başlangıçta 'kod' $ this-> store_result yoktu (); funcitondaki 'code', bazı araştırmalardan sonra ekledi ve yardımcı olabileceğini düşündü. –

+0

Parantezleri çıkarın. $ this -> _ result-> num_rows(); bunu $ -> _ sonuç-> num_rows; Benim için çalıştı –

cevap

6

Kılavuzdan, mysqli_result::num_rows bir işlev değil, satır sayısını içeren bir değişken gibi görünüyor.

Böyle kullanılabilir:

$num_rows = $mysqli_result->num_rows; 

fonksiyon eşdeğer sen mysqli_result nesne geçer mysqli_num_rows($result), ama sen usul tarzı yerine nesne yönelimli stili kullanıyorsanız bu. Kodunuzda

, sen

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

veya alternatif ise, (Ben bu hata mesajını alıyorsanız fonksiyonu olduğunu varsayıyorum) böyle olmak utils_MysqlImprovedResult sınıfta count() fonksiyonunu değiştirmelisiniz Eğer OO ve prosedürel stilleri (muhtemelen kötü bir fikir),

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

Burada nasıl kullanılmıyor değil mi? Elkitabını kontrol ettim ve bu, el kitabında da aynı şekilde kullanılıyor. –

+0

Kodunuzda "$ this -> _ sonuç-> num_rows();" var, bu el kitabında nasıl kullanıldığından değil. Kılavuzda "int $ mysqli_result-> num_rows;" (fonksiyon parantezlerinin eksik olduğuna dikkat edin)() "). "$ This return -> _ result-> num_rows;" ile değiştirin. kodunda ve işe yaramalı. –

+0

Teşekkür ederim ... Burada ilk sorum oldu. Zevk seninle iş yapmak. –

İlgili konular