你不能。该函数仅返回第一个结果。并且没有更多信息。
所以你需要返回一些不同的东西,例如结果资源:
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);
}
}