限制mysqli访问php数据
我的问题是我有一个很大的数据库(10000多行),我想用php和mysqli一次获得它们。但是当我用var_dump打印所有结果时,我只得到第一行。好像这是唯一发生的事情。所以我的问题是:php mysql连接有限制吗?或者这是我的代码中的错误 这是我的代码:限制mysqli访问php数据,php,mysql,mysqli,Php,Mysql,Mysqli,我的问题是我有一个很大的数据库(10000多行),我想用php和mysqli一次获得它们。但是当我用var_dump打印所有结果时,我只得到第一行。好像这是唯一发生的事情。所以我的问题是:php mysql连接有限制吗?或者这是我的代码中的错误 这是我的代码: $link = mysqli_connect("localhost","root","","ows_index2"); $info_wu = mysqli_query($link,"SELECT `hostname`, `page` fr
$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括号,登录很好,因为我得到了第一行)这是代码中的错误 这: 应该是:
while($row = mysqli_fetch_assoc($info_wu))
var_dump($row);
这是代码中的错误 这: 应该是:
while($row = mysqli_fetch_assoc($info_wu))
var_dump($row);
如果您有PHP5.3+,那么可以使用新的而不是mysqli_fetch_assoc()
如果您有PHP5.3+,那么可以使用新的而不是mysqli_fetch_assoc()
mysqli\u fetch\u assoc
一次只提取一行。而且没有任何东西可以使用mysqli
AFAIK(不是完整的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
现在是一个数组,每个元素包含一行。您也可以使用foreach
并单独处理每一行,而不是iterator\u to\u array
迭代器代码可能仅适用于您的情况,它是一个更通用的代码,可用于许多具有数据库结果操作的情况。一篇相关的博文是:展示了如何在同一个迭代器上多次迭代。
mysqli\u fetch\u assoc
一次只获取一行。而且没有任何东西可以使用mysqli
AFAIK(不是完整的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
现在是一个数组,每个元素包含一行。您也可以使用foreach
并单独处理每一行,而不是iterator\u to\u array
迭代器代码可能仅适用于您的情况,它是一个更通用的代码,可用于许多具有数据库结果操作的情况。一篇相关的博文是:这展示了如何在同一个迭代器上迭代多次。您需要迭代查询结果:)使用而不是
mysqli\u fetch\u assoc
。您需要迭代查询结果:)使用而不是mysqli\u fetch\u assoc
+1:我知道有类似的事情,但不知道函数的名称。+1:我知道有类似的东西,但不知道函数的名称。