수 없습니다. 이 함수는 첫 번째 결과 만 반환합니다. 그리고 더 이상의 정보는 없습니다. 그런 다음 그 위에 반복 할 수
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);
}
}
대신 PDO를 사용하면 결과를 쉽게 반복 할 수 있습니다. 또한 함수는 데이터베이스 연결이 누락되어 다중 연결에서 작동하지 않습니다. – hakre
mysql * 계열의 함수는 더 이상 사용되지 않습니다. 대신 [PDO] (http://php.net/manual/en/intro.pdo.php)를 사용하십시오. – nico
PDO 또는 mysqli를 사용해야합니다. mysqli 확장은 이전 mysql 확장과 거의 같다. PHP 문서에서 [Choose API] (http://www.php.net/manual/en/mysqlinfo.api.choosing.php)를 읽어보십시오. –