I have a function that fetches some rows based on the sql query into an array. That array is returned from a function, How can I traverse all the elements of the returned array? I am a beginner in Php.
我有一个函数,它将基于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);
It only prints the first element, or the first record !
它只打印第一个元素或第一个记录!
4 个解决方案
#1
4
Try something like this:
尝试这样的事情:
$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);
}
And in case you haven't already noticed it, someone will flame you about using MySQL instead of a different thing like MySQLI or PDO. Don't let them bother you. The point here is that your query returns potentially many rows, and you need some kind of iterator to retrieve all of the rows so your script can process them.
如果你还没有注意到它,有人会激起你使用MySQL而不是像MySQLI或PDO这样的东西。不要让他们打扰你。这里的要点是您的查询可能返回许多行,并且您需要某种迭代器来检索所有行,以便您的脚本可以处理它们。
In the instant case, there will be one product_id element in each $row array. Once you see the output of the print_r() function, it will probably be obvious how you might change the code. This might turn out to be what you want (I think).
在本例中,每个$ 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;
}
#2
2
You are returning the result of a single call to mysql_fetch_assoc()
so you will only ever get the first row.
您将返回一次调用mysql_fetch_assoc()的结果,因此您只能获得第一行。
Loop over them to create a multidimensional array, then return:
循环遍历它们以创建多维数组,然后返回:
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;
}
Now you can loop them with foreach
:
现在你可以使用foreach循环它们:
$list = get_user_wants(99);
foreach($list as $product)
{
print_r($product);
}
Side note: the mysql_*
library is deprecated, it is recommended to upgrade to MySQLi or PDO.
附注:不推荐使用mysql_ *库,建议升级到MySQLi或PDO。
#3
2
You can't. The function is only returning the first result. And no more information.
你不能。该函数仅返回第一个结果。没有更多的信息。
So you need to return something different, for example the result resource:
所以你需要返回不同的东西,例如结果资源:
function get_user_wants($user_id)
{
$query = mysql_query("select product_id from user_wishlist where user_id= $user_id");
return $query;
}
You can then iterate over it:
然后你可以迭代它:
$result = get_user_wants($user_id);
while ($out = mysql_fetch_assoc($result))
{
print_r($out):
}
A better way then is to wrap the result in an Iterator:
更好的方法是将结果包装在Iterator中:
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):
}
Such an result iterator could look like:
这样的结果迭代器看起来像:
/**
* 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);
}
}
#4
1
Try
尝试
foreach ($out as $key=>$value)
echo "$key: $value<br>";
as a starting point.
作为一个起点。
#1
4
Try something like this:
尝试这样的事情:
$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);
}
And in case you haven't already noticed it, someone will flame you about using MySQL instead of a different thing like MySQLI or PDO. Don't let them bother you. The point here is that your query returns potentially many rows, and you need some kind of iterator to retrieve all of the rows so your script can process them.
如果你还没有注意到它,有人会激起你使用MySQL而不是像MySQLI或PDO这样的东西。不要让他们打扰你。这里的要点是您的查询可能返回许多行,并且您需要某种迭代器来检索所有行,以便您的脚本可以处理它们。
In the instant case, there will be one product_id element in each $row array. Once you see the output of the print_r() function, it will probably be obvious how you might change the code. This might turn out to be what you want (I think).
在本例中,每个$ 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;
}
#2
2
You are returning the result of a single call to mysql_fetch_assoc()
so you will only ever get the first row.
您将返回一次调用mysql_fetch_assoc()的结果,因此您只能获得第一行。
Loop over them to create a multidimensional array, then return:
循环遍历它们以创建多维数组,然后返回:
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;
}
Now you can loop them with foreach
:
现在你可以使用foreach循环它们:
$list = get_user_wants(99);
foreach($list as $product)
{
print_r($product);
}
Side note: the mysql_*
library is deprecated, it is recommended to upgrade to MySQLi or PDO.
附注:不推荐使用mysql_ *库,建议升级到MySQLi或PDO。
#3
2
You can't. The function is only returning the first result. And no more information.
你不能。该函数仅返回第一个结果。没有更多的信息。
So you need to return something different, for example the result resource:
所以你需要返回不同的东西,例如结果资源:
function get_user_wants($user_id)
{
$query = mysql_query("select product_id from user_wishlist where user_id= $user_id");
return $query;
}
You can then iterate over it:
然后你可以迭代它:
$result = get_user_wants($user_id);
while ($out = mysql_fetch_assoc($result))
{
print_r($out):
}
A better way then is to wrap the result in an Iterator:
更好的方法是将结果包装在Iterator中:
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):
}
Such an result iterator could look like:
这样的结果迭代器看起来像:
/**
* 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);
}
}
#4
1
Try
尝试
foreach ($out as $key=>$value)
echo "$key: $value<br>";
as a starting point.
作为一个起点。