I have a problem developing this example code.
This code would help to understand how communication with a MySQL server works in FreeBASIC, Currently I have been able to find some examples in C but I don't understand how to adapt everything in FreeBASIC.
In the part where the content should be displayed gives me an error when compiling and I have no idea how to do it.
#Include Once "mysql\mysql.bi"
#define NULL 0
Dim Shared URLServer As String
Dim Shared SVRDataBase As String
Dim Shared SVRUser As String
Dim Shared SVRPassword As String
DIM Shared Conn As MYSQL PTR
DIM Shared MySQLOut As MYSQL_RES PTR
DIM Shared Row As MYSQL_ROW
Declare Function MySQLRead(MyQuery As String, MyWork As String) As Integer
Function MySQLRead(MyQuery As String, MyWork As String) As Integer
URLServer = "localhost"
SVRDataBase = "test"
SVRUser = "testuser"
SVRPassword = "testpassword"
MyQuery = "SELECT * FROM `test_table`"
Conn = mysql_init(NULL)
if (Conn = 0) then Print "Error Conn= 0"
if mysql_real_connect(Conn, URLServer, SVRUser, SVRPassword, SVRDataBase, 0, NULL, 0) = NULL then Print "Real Connect = 1"
if mysql_query(Conn, MyQuery) then Print "Query = 1"
MySQLOut = mysql_store_result(Conn)
if MySQLOut = 0 then Print "No out"
Return 1
End Function
Sub Reading()
DIM TotalCol As Integer
DIM TotalRow As Integer
Dim CiR As Integer
Dim CiC As Integer
Dim NE as Integer
IF MySQLRead("SELECT * FROM `cape_anagrafica`", "read") = 0 then Print "Errore"
TotalCol = mysql_num_fields(MySQLOut)
TotalRow = mysql_num_rows(MySQLOut)
print TotalRow
print MySQLOut(0)
mysql_free_result(MySQLOut)
mysql_close(Conn)
End Sub
Reading()
end 0
CodePudding user response:
The code is written right what is wrong is just the display, treat the mysql object as an array while it is a type object.
Below I will rewrite the correct code.
#Include Once "mysql\mysql.bi"
#define NULL 0
Dim Shared URLServer As String
Dim Shared SVRDataBase As String
Dim Shared SVRUser As String
Dim Shared SVRPassword As String
DIM Shared Conn As MYSQL PTR
DIM Shared MySQLOut As MYSQL_RES PTR
DIM Shared Row As MYSQL_ROW
Declare Function MySQLRead(MyQuery As String, MyWork As String) As Integer
Function MySQLRead(MyQuery As String, MyWork As String) As Integer
URLServer = "localhost"
SVRDataBase = "test"
SVRUser = "testuser"
SVRPassword = "testpassword"
MyQuery = "SELECT * FROM `test_table`"
Conn = mysql_init(NULL)
if (Conn = 0) then Print "Error Conn= 0"
if mysql_real_connect(Conn, URLServer, SVRUser, SVRPassword, SVRDataBase, 0, NULL, 0) = NULL then Print "Real Connect = 1"
if mysql_query(Conn, MyQuery) then Print "Query = 1"
MySQLOut = mysql_store_result(Conn)
if MySQLOut = 0 then Print "No out"
Return 1
End Function
Sub Reading()
DIM TotalCol As Integer
DIM TotalRow As Integer
Dim CiR As Integer
Dim CiC As Integer
Dim NE as Integer
IF MySQLRead("SELECT * FROM `cape_anagrafica`", "read") = 0 then Print "Errore"
TotalCol = mysql_num_fields(MySQLOut)
TotalRow = mysql_num_rows(MySQLOut)
print TotalRow
' remove print MySQLOut(0) because is not array
' and put this cicle FOR NEXT
' Remember Row[] is not a array if you want store the data in array you have ' to convert it
For CiR = 0 to TotalRow -1 step 1
Row = mysql_fetch_row(MySQLOut)
For Cic = 0 To TotalCol - 1 step 1
if Row[CiC] = NULL then print "| NULL "; else print "| "; *Row[CiC]
Next CiC
Next Cir
mysql_free_result(MySQLOut)
mysql_close(Conn)
End Sub
Reading()
end 0