News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

MySql and the loop

Started by Grincheux, June 01, 2011, 03:46:16 PM

Previous topic - Next topic

Grincheux

I have a very basic query to retrieve users from a table.
MySql retrieves the first record but stops declaring there are no more datas (mysql_next_result returns -1)

Here is the asm code :
QuoteUser_ReadEx                  PROC   __lpszQuery:LPSTR,__lpFunction:LPVOID,__lParam:LPARAM
                        LOCAL   _lpUser:LPF32_USER
                        LOCAL   _hResult:HANDLE

@@ :

                        INVOKE   mysql_next_result,hMySql

                        test   eax,eax
                        jz      @B

                        INVOKE   PhR_Memory_Alloc,SIZEOF F32_USER

                        test   eax,eax
                        jnz      @Begin

                           stc
                           ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@Begin :

                        mov      _lpUser,eax

                        INVOKE   lstrlen,__lpszQuery
                        INVOKE   mysql_real_query,hMySql,__lpszQuery,eax

                        test   eax,eax
                        jz      @Success

                           INVOKE   PhR_Memory_Free,_lpUser

                           call   mysql_errno

                           stc
                           ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@Error :

                        push   eax
                        INVOKE   PhR_Memory_Free,_lpUser
                        pop      eax

                        stc
                        ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@Success :

                        INVOKE   mysql_store_result,hMySql

                        test   eax,eax
                        jnz      @ReadyToGetData

                           call   mysql_errno

                           test   eax,eax
                           jz      @Error

                              INVOKE   mysql_field_count,hMySql

                              test   eax,eax
                              jnz      @ReadyToGetData

                                 mov      eax,_lpUser
                                 clc

                                 stc
                                 ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@ReadyToGetData :

                        mov      _hResult,eax

                        INVOKE   User_Get,eax,_lpUser

                        INVOKE   mysql_free_result,_hResult

                        push   _lpUser
                        push   __lParam
                        call   __lpFunction

                        INVOKE   mysql_next_result,hMySqlThe problem is HERE, the functions returns -1 and only shows 1 record on the three I have !

                        test   eax,eax
                        jz      @Success

                        INVOKE   PhR_Memory_Free,_lpUser

                        mov      eax,TRUE
                        clc
                        ret
User_ReadEx                  ENDP


Can someone help me ?
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

Tedd

I don't think mysql_next_result does what you expect - it's for getting the result of the next statement, you only have one statement(?)
You want to get each of the rows in your result: mysql_fetch_row

http://dev.mysql.com/doc/refman/5.6/en/c-api-functions.html
No snowflake in an avalanche feels responsible.

Grincheux

Thanks Tedd, now it is ok.

I did not undestand like this in fact it is :

First, you get a pointer onto the result
Second you make a loop with the pointer got until mysql_fetch_row returns NULL

I have only one statment, but I misunderstood (mixed) statment and query resukt. In fact statment is the result of one or more queries
Once got, just loop with mysql_fetch_row for this statment
For the next statment you must use mysql_next_result but don't forget to free the previous result using mysql_free_result

For me this is like a treeview.

I read somewhere that creating a dummy loop with mysql_next_result would ensure that all the previous queries are ended. Is it true ?

Here is the good code :

Quote
;===========================================================================================
                        ALIGN   16
;===========================================================================================

User_Get                  PROC   __hResult:DWord,__lpUser:LPF32_USER
                        LOCAL   _szTmp[1024]:Byte

                        push   ebx
                        push   edi
                        mov      ebx,__lpUser

                        mov      (F32_USER Ptr [ebx]).Pseudo_1.dwSubIndex,1
                        mov      (F32_USER Ptr [ebx]).Pseudo_2.dwSubIndex,2
                        mov      (F32_USER Ptr [ebx]).Pseudo_3.dwSubIndex,3
                        mov      (F32_USER Ptr [ebx]).Pseudo_4.dwSubIndex,4
                        mov      (F32_USER Ptr [ebx]).Pseudo_5.dwSubIndex,5

; 0  [F32Serveurs].[Utilisateurs].[_Index],

                        INVOKE   mysql_fetch_row,__hResult

                        test   eax,eax
                        jnz      @Success

                           xor      eax,eax

                           stc
                           ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@Success :

                        mov      edi,eax

                        INVOKE   atodw,DWord Ptr [edi]

                        mov      (F32_USER Ptr [ebx]).dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).EtatCivil.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Pseudo_1.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Pseudo_2.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Pseudo_3.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Pseudo_4.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Pseudo_5.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Adresse.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Internet.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Telephone.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Signature.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Avatar.dwIndexUser,eax
                        mov      (F32_USER Ptr [ebx]).Droits.dwIndexUser,eax

                        INVOKE   atodw,DWord Ptr [edi + 4]

                        mov      (F32_USER Ptr [ebx]).EtatCivil.dwIndexSexe,eax

                        INVOKE   atodw,DWord Ptr [edi + 8]

                        mov      (F32_USER Ptr [ebx]).EtatCivil.dwIndexCivilite,eax

                        mov      eax,[edi + 12]

                        test   eax,eax
                        jz      @F

                           lea      ecx,_szTmp

                           INVOKE   lstrcpy,ecx,eax

                           lea      edx,(F32_USER Ptr [ebx]).EtatCivil.DateDeNaissance
                           INVOKE   SqLite_DateToSystemDate,eax,edx

@@ :

                        mov      eax,[edi + 16]

                        test   eax,eax
                        jz      @F

                           lea      edx,(F32_USER Ptr [ebx]).EtatCivil.szNom
                           INVOKE   lstrcpy,edx,eax

@@ :

                        mov      eax,[edi + 20]

                        test   eax,eax
                        jz      @F

                           lea      edx,(F32_USER Ptr [ebx]).EtatCivil.szPrenom
                           INVOKE   lstrcpy,edx,eax

@@ :

                        mov      eax,ebx
                        pop      edi
                        pop      ebx
                        ret
User_Get                  ENDP

;===========================================================================================
                        ALIGN   16
;===========================================================================================

User_ReadEx                  PROC   __lpszQuery:LPSTR,__lpFunction:LPVOID,__lParam:LPARAM
                        LOCAL   _lpUser:LPF32_USER
                        LOCAL   _hResult:HANDLE

@@ :

                        INVOKE   mysql_next_result,hMySql

                        test   eax,eax
                        jz      @B

                        INVOKE   PhR_Memory_Alloc,SIZEOF F32_USER

                        test   eax,eax
                        jnz      @Begin

                           stc
                           ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@Begin :

                        mov      _lpUser,eax

                        INVOKE   lstrlen,__lpszQuery
                        INVOKE   mysql_real_query,hMySql,__lpszQuery,eax

                        test   eax,eax
                        jz      @Success

                           INVOKE   PhR_Memory_Free,_lpUser

                           call   mysql_errno

                           stc
                           ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@Error :

                        push   eax
                        INVOKE   PhR_Memory_Free,_lpUser
                        pop      eax

                        stc
                        ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@Success :

                        INVOKE   mysql_store_result,hMySql

                        test   eax,eax
                        jnz      @ReadyToGetData

                           call   mysql_errno

                           test   eax,eax
                           jz      @Error

                              INVOKE   mysql_field_count,hMySql

                              test   eax,eax
                              jnz      @ReadyToGetData

                                 INVOKE   PhR_Memory_Free,_lpUser

                                 mov      eax,TRUE

                                 clc
                                 ret

;-------------------------------------------------------------------------------------------
                        ALIGN   16
;-------------------------------------------------------------------------------------------

@ReadyToGetData :

                        mov      _hResult,eax

@Loop :

                        INVOKE   User_Get,_hResult,_lpUser
                        jc      @EndLoop

                           push   _lpUser
                           push   __lParam
                           push   OFFSET @Loop
                           jmp      __lpFunction   ; CALL TO User_Get

@EndLoop :

                        INVOKE   mysql_free_result,_hResult
                        INVOKE   PhR_Memory_Free,_lpUser

                        mov      eax,TRUE

                        clc
                        ret
User_ReadEx                  ENDP

I am very pleased. I did not find any help file for mysql, written in French, this my error...

Thanks again.

Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz