2012-04-15 4 views
1

내 문제는 내가 큰 DB를 (10000 + 행)하고 PHP와 mysqli 그들 모두를 한꺼번에하고 싶어. 하지만 var_dump 모든 결과를 인쇄 할 때 나는 단지 행을 얻을. 그것은 그것이 끝난 유일한 것입니다. 그래서 내 질문은 : php mysql 연결에 제한이 있습니까? 또는 내 코드에서 오류가 발생했습니다.PHP 데이터에 mysqli에 대한 제한

$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

쿼리 결과를 반복해야합니다. –

+0

mysqli_fetch_assoc 대신 [mysqli_fetch_all'] (http://php.net/manual/mysqli-result.fetch-all.php)을 사용하십시오. – hakre

답변

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_assoc의 새로운 mysqli_fetch_all()을 사용할 수 있습니다().

$rows = mysqli_fetch_all($info_wu, MYSQLI_ASSOC); 

var_dump($rows); 
+0

+1 : 그와 비슷한 것이 있지만 기능의 이름을 알지 못했습니다. – hakre

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입니다. 동일한 반복자를 여러 번 반복하는 방법을 보여줍니다.

관련 문제