mysql_fetch_assoc只获取一条记录

Man*_*023 6 php mysql

我有一个包含查询结果的数组:

.....some code.....
$result = mysql_query("SELECT * FROM vttest");
$row = mysql_fetch_assoc($result);
print_r($row);
Run Code Online (Sandbox Code Playgroud)

但它只打印一条记录,我不能把$ row放在一个循环中,因为我将在稍后对该变量进行排序.

Array
(
    [id] => 3
    [rep] => Mike
    [name] => Joe
    [department] => Business
    [location] => Room 1
    [email] => xxxx@hotmail.com
    [phone] => 519-123-4567
    [type] => Visit
    [drink] => Coffee
    [notes] => Some notes
    [lastVisited] => 2010-08-27
    [nextVisit] => 2010-08-27
)
Run Code Online (Sandbox Code Playgroud)

我知道有更多的记录,我怎么能打印所有它仍然能够使用$ row变量?

Sar*_*raz 13

您实际上是获得一条记录,因为您需要使用循环来获取所有记录:

$my_arary = array();
while($row = mysql_fetch_assoc($result)){
  $my_arary[] = $row;
  print_r($row);
}
Run Code Online (Sandbox Code Playgroud)

正如你所说,以后你可以用它$my_arary来做任何你喜欢的事情.