1

私の問題は、大きなデータベース (10000 行以上) があり、php と mysqli を使用してそれらを一度に取得したいということです。しかし、var_dump を使用してすべての結果を出力すると、最初の行しか取得できません。それがやってきた唯一のもののようです。だから私の質問は: php mysql 接続に制限はありますか? それとも私のコードのエラーですか?

これは私のコードです:

$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ブラケットを使用すると、最初の行を取得できるため、ログインは良好です)

4

3 に答える 3

2

これはコードのエラーです。

これ:

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

次のようにする必要があります。

while($row = mysqli_fetch_assoc($info_wu))
    var_dump($row);
于 2012-04-15T22:31:32.950 に答える
2

PHP 5.3+ を使用している場合は、 mysqli_fetch_assoc()の代わりに新しいmysqli_fetch_all()を使用できます。

$rows = mysqli_fetch_all($info_wu, MYSQLI_ASSOC);

var_dump($rows);
于 2012-04-15T22:41:42.730 に答える
1

mysqli_fetch_assoc一度に 1 行だけをフェッチします。そして、AFAIKですべての行をフェッチするものはありませんmysqli(完全なmysqli proではありません)。(編集:あります

これをイテレータに変換し、そのイテレータを配列のように使用することをお勧めします。

/**
 * 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は、各要素ごとに 1 つの行を含む配列になりました。代わりに、各行を独自にiterator_to_array使用して処理することもできます。foreach

イテレータ コードは、あなたのケースだけでは少し多すぎるように見えるかもしれません。これは、データベースの結果操作で多くのケースに使用できる、より一般的なものです。関連するブログ投稿は次のとおりです。同じイテレーターを複数回反復する方法を示すPHP イテレーターの楽しみ。

于 2012-04-15T22:42:51.513 に答える