Why you should not use mysql_fetch_assoc more than 1 time?

后端 未结 4 961
梦毁少年i
梦毁少年i 2020-12-20 19:09

Some people say you should not use mysql_fetch_assoc more than one time, why is that?

e.g.: I want to display two tables one with users who paid for mem

4条回答
  •  时光说笑
    2020-12-20 20:06

    Quote by Typer85 (link):

    Please be advised that the resource result that you pass to this function can be thought of as being passed by reference because a resource is simply a pointer to a memory location.

    Because of this, you can not loop through a resource result twice in the same script before resetting the pointer back to the start position.

    For example:

    
    

    The only solution to this is to reset the pointer to make it point at the first row again before the second code segment, so now the complete code will look as follows:

    
    

    Of course you would have to do extra checks to make sure that the number of rows in the result is not 0 or else mysql_data_seek itself will return false and an error will be raised.

    Also please note that this applies to all functions that fetch result sets, including mysql_fetch_row, mysql_fetch_assos, and mysql_fetch_array.

提交回复
热议问题