adriancs35 Posted December 13, 2014 Share Posted December 13, 2014 Hi, so today I just started learning this language, and I'm trying to make my script with parts of example scripts from https://www.autoitscript.com/autoit3/docs/functions. I'm trying to run a mysql query and I'm getting "Variable Undeclared" error even if it declared as a Global one (atleast i think i declared it as one..) Basically what my script should do is make an ID for every machine in my LAN store it into a txt file in AppData and then insert it into a db. #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include "EzMySql.au3" #include <Array.au3> Example() Func Example() $id = Random(1, 1000, 1);Numar random de la 1 la 100 Local Const $sFilePath = @AppDataDir & "id.txt" ; Selectare %appdata% si id.txt Local $iFileExists = FileExists($sFilePath) If $iFileExists Then Else ; Create a temporary file to write data to. If Not FileCreate($sFilePath, $ID & @CRLF) Then Return MsgBox($MB_SYSTEMMODAL, "", "O eroare s-a produs in timp ce se scria fila temporara") ; Open the file for writing (append to the end of a file) and store the handle to a variable. Global $hFileOpen = FileOpen($sFilePath, $FO_APPEND) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "O eroare s-a produs in timp ce se citea fila.") Return False EndIf ; Read the contents of the file using the handle returned by FileOpen. Global $sFileRead = FileRead($hFileOpen) ; Close the handle returned by FileOpen. FileClose($hFileOpen) EndIf EndFunc ; Create a file. Func FileCreate($sFilePath, $sString) Local $bReturn = True ; Create a variable to store a boolean value. If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False. Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite. EndFunc ;==>FileCreate $name=@ComputerName If Not _EzMySql_Startup() Then MsgBox(0, "Error Starting MySql", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf If Not _EzMySql_Open("127.0.0.1", "root", "", "vrgaming", "3306") Then MsgBox(0, "Error opening Database", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf If Not _EzMySql_Exec("INSERT INTO `lan` (id, nume) VALUES ('"& $sFileRead &"', '"& $name &"')") Then MsgBox(0, "Error opening Database", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf _EzMySql_Close() _EzMySql_ShutDown() Exit What is wrong with the $sFileRea variable? Link to comment Share on other sites More sharing options...
Jfish Posted December 13, 2014 Share Posted December 13, 2014 Hello and welcome to the forum. If you use the code tags for your code (to the left of the quote) it is much more readable. Also, can you copy paste the exact error message you are getting? Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
Bowmore Posted December 13, 2014 Share Posted December 13, 2014 It's generally not a good idea to declare Global variables within a function as this can result, as was the case in your code, in the variable being used before it is declared. You should normally declare global somewhere near the top of your code before any function or code that uses the global variables is run. Have a close look at the code below which I've rearranged slightly. It should run now, although I did not download the DLL to test it; expandcollapse popup#include <MsgBoxConstants.au3> #include <FileConstants.au3> #include "EzMySql.au3" #include <Array.au3> Global $hFileOpen = 0 Global $sFileRead = "" Global $name = @ComputerName Example() If Not _EzMySql_Startup() Then MsgBox(0, "Error Starting MySql", "Error: " & @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf If Not _EzMySql_Open("127.0.0.1", "root", "", "vrgaming", "3306") Then MsgBox(0, "Error opening Database", "Error: " & @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf If Not _EzMySql_Exec("INSERT INTO `lan` (id, nume) VALUES ('" & $sFileRead & "', '" & $name & "')") Then MsgBox(0, "Error opening Database", "Error: " & @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf _EzMySql_Close() _EzMySql_ShutDown() Exit Func Example() $id = Random(1, 1000, 1);Numar random de la 1 la 100 Local Const $sFilePath = @AppDataDir & "\id.txt" ; Selectare %appdata% si id.txt Local $iFileExists = FileExists($sFilePath) If Not $iFileExists Then ; Create a temporary file to write data to. If Not FileCreate($sFilePath, $id & @CRLF) Then Return MsgBox($MB_SYSTEMMODAL, "", "O eroare s-a produs in timp ce se scria fila temporara") EndIf ; Open the file for writing (append to the end of a file) and store the handle to a variable. $hFileOpen = FileOpen($sFilePath, $FO_APPEND) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "O eroare s-a produs in timp ce se citea fila.") Return False EndIf ; Read the contents of the file using the handle returned by FileOpen. $sFileRead = FileRead($hFileOpen) ; Close the handle returned by FileOpen. FileClose($hFileOpen) EndFunc ;==>Example ; Create a file. Func FileCreate($sFilePath, $sString) Local $bReturn = True ; Create a variable to store a boolean value. If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False. Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite. EndFunc ;==>FileCreate "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook Link to comment Share on other sites More sharing options...
adriancs35 Posted December 14, 2014 Author Share Posted December 14, 2014 Hello and welcome to the forum. If you use the code tags for your code (to the left of the quote) it is much more readable. Also, can you copy paste the exact error message you are getting? Thanks for the warm welcome to the forum . It's generally not a good idea to declare Global variables within a function as this can result, as was the case in your code, in the variable being used before it is declared. You should normally declare global somewhere near the top of your code before any function or code that uses the global variables is run. Have a close look at the code below which I've rearranged slightly. It should run now, although I did not download the DLL to test it; Thanks, that almost solved me out, except it was introducing in db $id before it was given the value by the function, i solved it like this: expandcollapse popup#include <MsgBoxConstants.au3> #include <FileConstants.au3> #include "EzMySql.au3" #include <Array.au3> Global $hFileOpen = 0 Global $sFileRead = "" Global $name = @ComputerName Example() Func Example() $id = Random(1, 1000, 1);Numar random de la 1 la 100 Local Const $sFilePath = @AppDataDir & "\id.txt" ; Selectare %appdata% si id.txt Local $iFileExists = FileExists($sFilePath) If Not $iFileExists Then ; Create a temporary file to write data to. If Not FileCreate($sFilePath, $id & @CRLF) Then Return MsgBox($MB_SYSTEMMODAL, "", "O eroare s-a produs in timp ce se scria fila temporara") EndIf ; Open the file for reading and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePath, $FO_READ) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Return False EndIf ; Read the contents of the file using the handle returned by FileOpen. $sFileRead = FileRead($hFileOpen) ; Close the handle returned by FileOpen. FileClose($hFileOpen) EndFunc ;==>Example ; Create a file. Func FileCreate($sFilePath, $sString) Local $bReturn = True ; Create a variable to store a boolean value. If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False. Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite. EndFunc ;==>FileCreate If Not _EzMySql_Startup() Then MsgBox(0, "Error Starting MySql", "Error: " & @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf If Not _EzMySql_Open("127.0.0.1", "root", "", "vrgaming", "3306") Then MsgBox(0, "Error opening Database", "Error: " & @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf If Not _EzMySql_Exec("INSERT INTO `bots` (id, nume) VALUES ('" & $sFileRead & "', '" & $name & "')") Then MsgBox(0, "Error opening Database", "Error: " & @error & @CR & "Error string: " & _EzMySql_ErrMsg()) Exit EndIf _EzMySql_Close() _EzMySql_ShutDown() Exit Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now