Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么不应该使用mysql\u fetch\u assoc超过一次?_Php_Mysql_Sql_Request - Fatal编程技术网

Php 为什么不应该使用mysql\u fetch\u assoc超过一次?

Php 为什么不应该使用mysql\u fetch\u assoc超过一次?,php,mysql,sql,request,Php,Mysql,Sql,Request,有人说你不应该多次使用mysql\u fetch\u assoc,为什么 e、 g:我想显示两个表,一个是为会员付费的用户,另一个是没有付费的用户,所以我不需要两次查询数据库,而是一次查询数据库,得到两种类型的用户的$result变量,然后运行loopmysql\u fetch\u assoc并查看list['membership']='paid'然后返回 第二次我循环mysql\u fetch\u assoc并查看列表['membership']='free',然后回显 考虑到我得到的注册用户

有人说你不应该多次使用mysql\u fetch\u assoc,为什么

e、 g:我想显示两个表,一个是为会员付费的用户,另一个是没有付费的用户,所以我不需要两次查询数据库,而是一次查询数据库,得到两种类型的用户的
$result
变量,然后运行loop
mysql\u fetch\u assoc
并查看
list['membership']='paid'
然后返回

第二次我循环mysql\u fetch\u assoc并查看
列表['membership']='free'
,然后回显


考虑到我得到的注册用户和未注册用户数量大致相等,什么使用更少的资源。

当有人说你不能打两次电话时,他们的意思是针对同一资源。您传递给
mysql\u fetch\u assoc()
的资源结果是通过引用完成的。您需要重置指针的位置,然后才能再次使用
mysql\u fetch\u assoc()

编辑:为此,请尝试使用。

按类型引用85():

请注意,传递给此函数的资源结果可以被认为是通过引用传递的,因为资源只是指向内存位置的指针

因此,在将指针重置回起始位置之前,不能在同一脚本中循环两次资源结果

例如:


看来您要做的是将查询结果作为数组(字段)的数组(行)处理。但这并不是mysql库真正提供的。实际上,我经常做的是将行复制到数组中(只需在mysql_fetch上循环直到为空),然后使用PHP为此提供的数组函数对自己的行集执行我想要的操作。这还可以最大限度地减少表被锁定的时间。

将查询结果集想象成一根香肠,将mysql_fetch_assoc()想象成一把刀,可以切下一片香肠。每次你拿一排,另一片香肠就会被切掉,而且总是一片新的香肠。你不能去切下之前切下的一块,因为它已经被吃掉了。

typer85的例子似乎没有参数2,这是mysql\u data\u seekTHX给Ina所需要的,Ina为我指出了正确的方向:mysql\u data\u seek($results,0)需要位置。此外,上面的代码中缺少a)该函数的参数2是什么?@ina如果您的意思是
mysql\u data\u seek()
,则它是要查找的行号。我在线编辑了PHP手册的链接。由于某些原因,将其设置为0会产生以下错误:
Offset 0对于MySQL结果索引8无效(或者查询数据未缓冲)
<?php

// Assume We Already Queried Our Database.

// Loop Through Result Set.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// We looped through the resource result already so the
// the pointer is no longer pointing at any rows.

// If we decide to loop through the same resource result
// again, the function will always return false because it
// will assume there are no more rows.

// So the following code, if executed after the previous code
// segment will not work.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// Because $queryContent is now equal to FALSE, the loop
// will not be entered.

?>
<?php

// Assume We Already Queried Our Database.

// Loop Through Result Set.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// Reset Our Pointer.

mysql_data_seek( $queryResult );

// Loop Again.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

?>