Page 1 of 1

Online MySQL DataBase How?

Posted: Sat Nov 19, 2005 6:48 am
by guruk
I do experimenting now with SQLITE and was working before a bit with SQL Statements.

Now I think about to do a connection to my Online MySQL Database.

How? Is there another Way than by sending Http Commands to a CGi?

If so... is there a standart CGI with MySQL that I can use to send direct Commands to my Database?

:) thanks a lot for attention

Regards
Chris

-- :oops: Birthday today :D

Posted: Sat Nov 19, 2005 1:07 pm
by Nik
Under Windows you can put your MySQL Database in the ODBC Connector located in the System configuration can't say how it's named because I'm using a German Version here Its under "Systemsteuerung"\"Verwaltung"\"Datenquellen(ODBC)"
After yo added your Database there you just need PBs Standard ODBC Commands and the name of the database and table (No server ip or anything like that within your PB Code). It's really simple and it doesn't matter wich database format you use. It working like this with Access MySQL and others.
bye Nik

Posted: Sat Nov 19, 2005 6:23 pm
by Thalius
Restored from an old Post using libmysql.dll
I'm not sure if that code is already sophisticated enough to present it in this forum, but maybe it's useful for some and - my personal hope - hopefully it benefits from your brain power...

This code connects to a MySQL server, using the libmysql.dll. You can download it from here, together with the "MySQL Server Books Online" that provides you with an overview and explanation of the functions.

http://www.mysqltools.com/download.htm

The database connection used in this example is valid, so you can try out instantly with the given parameters. Though the databases on the host are not valueable, please be careful when messing with the server.

#libmysql = 1

host.s = "62.75.148.207"
user.s = "purebasic"
passwd.s = "test"
db.s = "purebasic"
port.l = 3306

dbHnd.l
SQL.s
row.s
i.l
j.l
affRows.l
fieldNum.l
rowsNum.l

Procedure.s GetError(db_ID,requester)

Protected Errormsg.s, i.l, Error.l

If CallFunction(#libmysql,"mysql_errno",db_ID) > 0
*Error =CallFunction(#libmysql,"mysql_error",db_ID)
i=-1
Repeat
i=i+1
Errormsg=Errormsg+PeekS(*Error+i,1)
Until PeekB(*Error+i)=0
If requester
Result= MessageRequester("MySQL error",Errormsg,#PB_MessageRequester_Ok)
EndIf
EndIf

ProcedureReturn Errormsg

EndProcedure



If OpenLibrary(#libmysql,"libmysql.dll")
Result=CallFunction(#libmysql,"mysql_init",dbHnd)
If Result
dbHnd = Result
If CallFunction(#libmysql,"mysql_real_connect",dbHnd, host, user, passwd, db, port, "", 0) = 0
GetError(dbHnd,1)
Else
CallDebugger
SQL = "SELECT * FROM test"
If CallFunction(#libmysql,"mysql_real_query", dbHnd, SQL, Len(SQL))
GetError(dbHnd,1)
Else
*mysqlResult=CallFunction(#libmysql,"mysql_store_result",dbHnd)

;no result returned
If *mysqlResult=0
;no fields returned means error
If CallFunction(#libmysql,"mysql_field_count",dbHnd)
GetError(dbHnd,1)
;fields are returned, so no error but query didn't return data
Else

EndIf
;results are returned
Else


affRows = CallFunction(#libmysql,"mysql_affected_rows",dbHnd)
fieldNum = CallFunction(#libmysql,"mysql_num_fields",*mysqlResult)
rowsNum = CallFunction(#libmysql,"mysql_num_rows",*mysqlResult)

Debug affRows
Debug fieldNum
Debug rowsNum


For i=1 To rowsNum
*mysqlRow=CallFunction(#libmysql,"mysql_fetch_row",*mysqlResult)
*mysqlLen=CallFunction(#libmysql,"mysql_fetch_lengths",*mysqlResult)

row = ""

;length of given field
For j=1 To fieldNum
length=PeekL(*mysqlLen+4*(j-1))
fieldptr=PeekL(*mysqlRow+4*(j-1))
If fieldptr>0
content.s=PeekS(fieldptr,length)
Else
;zero pointer returend means empty field
content="NULL"
EndIf
row = row + content + ";"
Next j

Debug row

Next i
result.l=CallFunction(#libmysql,"mysql_free_result",*mysqlResult)
EndIf
EndIf
EndIf
EndIf
EndIf



I know that there is room for a lot of improvement (like when getting the error message char for char instead of it as a whole) & criticism - but that is what I'm hoping for.

Max.

Posted: Sun Nov 20, 2005 1:15 am
by guruk
Thats great Stuff.

thanks a lot guys for Energy.

Will check it out.

For the first Poster... its Online- not Offline.

As I see is the Soft on MySql not Public Domain is there any
free Lib also?

Greets from Dahab, Red Sea

Posted: Mon Nov 21, 2005 4:09 am
by Thalius
The old version lib is Free. Gonna see if i find my version somewhere in case this one doesent work.

Altho heres the link to the newest MySQL 5 Installer win32
http://mirrors.sunsite.dk/mysql/Downloa ... -win32.zip

Thalius

Posted: Mon Nov 21, 2005 3:59 pm
by Nik
That it's onlien doesn't matter you cann add mySQL servers in the ODBC Control in the same way as with offline ones or Acceess dbs