2012-12-30 82 views
0

我有一个函数可以将基于sql查询的一些行提取到一个数组中。 该数组是从函数返回的,如何遍历返回数组的所有元素? 我是一个初学者在Php。如何打印从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); 

它只打印第一个元素或第一个记录!

+0

使用PDO代替,然后您可以轻松地迭代结果。此外,你的功能缺少数据库连接,因此它不适用于多个连接。 – hakre

+0

不推荐使用mysql *系列函数。改为使用[PDO](http://php.net/manual/en/intro.pdo.php)。 – nico

+0

您应该使用PDO或mysqli。 mysqli扩展名与旧的mysql扩展名几乎相同。请阅读PHP文档中的[选择API](http://www.php.net/manual/en/mysqlinfo.api.choosing.php)。 –

回答

1

尝试

foreach ($out as $key=>$value) 
    echo "$key: $value<br>"; 

为出发点。

+2

不会给你任何东西print_r没有做过 –

+0

你的例子只会循环由'mysql_fetch_assoc'给出的第一行的列,因为它返回一个与取出的行相对应的关联数组,并将内部数据指针向前移动,一次只有一个鱼子。 –

+0

是的,它不会给print_r做任何事情 - 但这是关键:我把OQ读为“如何看看print_r()'blackbox” - 在大多数情况下,你不会只想要以回应内容。这种解释可能是错误的......在这种情况下,对OQ的编辑可能是好的 –

4

尝试这样:

$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; 
} 
+0

请随时纠正我的分析错误 - while()迭代器上的不平衡圆括号。 –

+0

谢谢你,在stackoverflow的人是非常好的web开发:) – Faizan

+0

其实我现在工作,基本上我想操纵接收数组之外的函数。无论如何它现在完成了。 – Faizan

2

要返回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。

+0

它为每个或while循环使用有什么不同? – Faizan

+0

使用while来获取行,然后当你有一个标准的php数组时,你可以使用for each循环。 – MrCode

2

你不能。该函数仅返回第一个结果。并没有更多的信息。

所以你需要返回不同的东西,例如,结果资源:

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); 
    } 
} 
+0

谢谢,我通常会寻求最简单的解决方案,我想这就是我想要的。 – Faizan

+0

@Faizan:对于一个非常简单的解决方案,请继续阅读这里:http://stackoverflow.com/a/11580420/367456 - 也看看PDO。它会让你的生活更轻松。 – hakre

相关问题