我有一个函数可以根据sql查询将一些行提取到一个数组中。该数组是从函数返回的,我如何遍历返回的数组的所有元素?我是Php的初学者。
function get_user_wants($user_id)
{
$query=mysql_query("select product_id from user_wishlist where user_id= $user_id");
return mysql_fetch_assoc($query);
}
$out=get_user_wants($user_id);
print_r($out);它只打印第一个元素,或者第一条记录!
发布于 2012-12-31 01:52:34
尝试如下所示:
$sql = "select product_id from user_wishlist where user_id= $user_id";
$res = mysql_query($sql) or die("FAIL: $sql BECAUSE: " . mysql_error());;
while ($row = mysql_fetch_assoc($res))
{
print_r($row);
}如果你还没有注意到这一点,有人会对你使用MySQL而不是像MySQLI或PDO这样的不同东西感到愤怒。不要让他们打扰你。这里的要点是,您的查询可能返回许多行,并且您需要某种迭代器来检索所有行,以便您的脚本可以处理它们。
在本例中,每个$row数组中都有一个product_id元素。一旦您看到了print_r()函数的输出,您可以很容易地知道如何更改代码。这可能会成为你想要的(我认为)。
function get_user_wants($user_id)
{
$sql = "select product_id from user_wishlist where user_id= $user_id";
$res = mysql_query($sql) or die("FAIL: $sql BECAUSE: " . mysql_error());;
while ($row = mysql_fetch_assoc($res))
{
$out[] = $row['product_id'];
}
return $out;
}发布于 2012-12-31 01:54:04
您将返回对mysql_fetch_assoc()的一次调用的结果,因此您将只获得第一行。
循环遍历它们以创建多维数组,然后返回:
function get_user_wants($user_id)
{
$query=mysql_query("select product_id from user_wishlist where user_id= $user_id");
$rows = array();
while($row = mysql_fetch_assoc($query))
{
$rows[] = $row;
}
return $rows;
}现在,您可以使用foreach循环它们
$list = get_user_wants(99);
foreach($list as $product)
{
print_r($product);
}附注:mysql_*库已弃用,建议升级到MySQLi或PDO。
发布于 2012-12-31 01:54:10
你不能。这个函数只返回第一个结果。也没有更多的信息。
因此,您需要返回一些不同的内容,例如result资源:
function get_user_wants($user_id)
{
$query = mysql_query("select product_id from user_wishlist where user_id= $user_id");
return $query;
}然后,您可以对其进行迭代:
$result = get_user_wants($user_id);
while ($out = mysql_fetch_assoc($result))
{
print_r($out):
}一种更好的方法是将结果包装在迭代器中:
function get_user_wants($user_id)
{
$query = mysql_query("select product_id from user_wishlist where user_id= $user_id");
return new MySqlResult($query);
}
$result = get_user_wants($user_id);
foreach ($result as $out)
{
print_r($out):
}这样的结果迭代器可能如下所示:
/**
* MySql Result Set - Array Based
*/
class MySqlResult implements Iterator, Countable
{
private $result;
private $index = 0;
private $current;
public function __construct($result)
{
$this->result = $result;
}
public function fetch($result_type = MYSQL_BOTH)
{
$this->current = mysql_fetch_array($this->result, $result_type);
return $this->current;
}
/**
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return array
*/
public function current()
{
return $this->current;
}
public function next()
{
$this->current && $this->fetch();
}
/**
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
*/
public function key()
{
return $this->current ? $this->index : null;
}
/**
* 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()
{
return (bool)$this->current;
}
/**
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind()
{
$this->fetch();
}
/**
* Count of rows.
*
* @link http://php.net/manual/en/countable.count.php
* @return int The count of rows as an integer.
*/
public function count()
{
return mysql_num_rows($this->result);
}
}https://stackoverflow.com/questions/14093052
复制相似问题