2012-04-15 15 views
1

我的問題是,我有一個大的分貝(10000+行),並希望一次與PHP和mysqli一起得到它們。但是當我用var_dump打印所有的結果時,我只得到了行。這就像是唯一過來的事情。所以我的問題是:是否有一個PHP的MySQL連接的限制?或者它是我的代碼中的錯誤。限制在mysqli到PHP數據

這是我的代碼:

$link = mysqli_connect("localhost","root","","ows_index2"); 
$info_wu = mysqli_query($link,"SELECT `hostname`, `page` from `pages`"); 
$row = mysqli_fetch_assoc($info_wu); 
var_dump $row; 

(用PHP支架和在日誌中還是不錯的,因爲我得到的第一行)

+2

您需要遍歷查詢的結果:) – 2012-04-15 22:31:26

+0

使用['mysqli_fetch_all'](http://php.net/manual/mysqli-result.fetch-all.php)而不是'mysqli_fetch_assoc'。 – hakre 2012-04-15 22:52:47

回答

2

它是在你的代碼中的錯誤。

此:

$row = mysqli_fetch_assoc($info_wu); 
var_dump($row); 

應該是:

while($row = mysqli_fetch_assoc($info_wu)) 
    var_dump($row); 
2

如果你有PHP 5.3+,你可以使用新的mysqli_fetch_all()代替mysqli_fetch_assoc()。

$rows = mysqli_fetch_all($info_wu, MYSQLI_ASSOC); 

var_dump($rows); 
+0

+1:我知道有類似的東西,但不知道函數的名字。 – hakre 2012-04-15 22:46:15

1

mysqli_fetch_assoc一次只取一行。 並沒有任何東西取得所有行與 mysqli AFAIK(不是一個完整的mysqli專業版)。 (編輯:There is

我建議你把它變成一個迭代,然後使用迭代器像一個數組:

/** 
* Iterator that fetches each iteration value from a 
* function until it is not string and equals false. 
*/ 
class FetchIterator extends NoRewindIterator 
{ 
    /** 
    * @var string 
    */ 
    private $fetchCallback; 
    /** 
    * number of the current iteration 
    * @var int 
    */ 
    private $virtual; 
    /** 
    * cache of the current value 
    * @var mixed 
    */ 
    private $current; 

    /** 
    * @param string $fetchCallback 
    */ 
    public function __construct($fetchCallback) 
    { 
     $this->fetchCallback = $fetchCallback; 
     $this->virtual = 0; 
    } 

    /** 
    * Return the current element 
    * @link http://php.net/manual/en/iterator.current.php 
    * @return mixed Can return any type. 
    */ 
    public function current() 
    { 
     $this->virtual || $this->next(); 
     return $this->current; 
    } 

    /** 
    * Return the key of the current element 
    * @link http://php.net/manual/en/iterator.key.php 
    * @return scalar scalar on success, integer 
    * 0 on failure. 
    */ 
    public function key() 
    { 
     $this->virtual || $this->next(); 
     return $this->virtual - 1; 
    } 

    /** 
    * Checks if current position is valid 
    * @link http://php.net/manual/en/iterator.valid.php 
    * @return boolean The return value will be casted to boolean and then evaluated. 
    * Returns true on success or false on failure. 
    */ 
    public function valid() 
    { 
     $this->virtual || $this->next(); 
     return $this->validate(); 
    } 

    /** 
    * @return bool 
    */ 
    private function validate() 
    { 
     return FALSE != $this->current || is_string($this->current); 
    } 

    /** 
    * Move forward to next element 
    * @link http://php.net/manual/en/iterator.next.php 
    * @return void Any returned value is ignored. 
    */ 
    public function next() 
    { 
     if ($this->virtual && ! $this->validate()) { 
      return; 
     } 
     $this->fetch(); 
     $this->virtual++; 
    } 

    /** 
    * fetch value from callback. can be called 
    * after assigning a new callback while 
    * in iteration. 
    */ 
    public function fetch() 
    { 
     $func = $this->fetchCallback; 
     $this->current = $func(); 
    } 

    /** 
    * number of times the fetch callback function 
    * has been called so far. 
    * 
    * @return int 
    */ 
    public function getCallCount() 
    { 
     return $this->virtual; 
    } 

    /** 
    * @return callback 
    */ 
    public function getFetchCallback() 
    { 
     return $this->fetchCallback; 
    } 

    /** 
    * Set callback for subsequent iterations. 
    * 
    * @param callback $fetchCallback 
    * @return FetchIterator 
    */ 
    public function setFetchCallback($fetchCallback) 
    { 
     $this->fetchCallback = $fetchCallback; 
     return $this; 
    } 
} 

用法:

$info_wu = mysqli_query($link,"SELECT `hostname`, `page` from `pages`"); 
$fetchFunction = function() use ($info_wu) { 
    return mysqli_fetch_assoc($info_wu); 
} 
$it = new FetchIterator($fetchFunction); 
$rows = iterator_to_array($it); 

變量$rows現在是一個數組每個元素包含一行。您也可以使用iterator_to_array而不是foreach,並自行處理每一行。

迭代器代碼對於您的案例可能看起來有點太多,這是一個更通用的代碼,可以用於多數情況下的數據庫結果操作。相關的博客文章是:Some PHP Iterator Fun,它顯示瞭如何多次遍歷相同的迭代器。