Solved: mysql_fetch_row(MySQLOut)

New to FreeBASIC? Post your questions here.
Post Reply
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Solved: mysql_fetch_row(MySQLOut)

Post by exagonx »

Hello
I have one little problem

In the server MySQL Database I have this table:

Code: Select all

Dim FQuery as String
FQuery = "CREATE TABLE IF NOT EXISTS `" & FDBPrefix & "personamarket_magazzino` (" & _
	"`id` INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, " & _
	"`CodMag` VARCHAR(15)CHARACTER SET utf8, " & _
	"`CodBarre` VARCHAR(40)CHARACTER SET utf8, " & _
	"`Descrizione` VARCHAR(50)CHARACTER SET utf8, " & _
	"`Fornitore` VARCHAR(50)CHARACTER SET utf8, " & _
	"`IdFornitore` INT(6), " & _
	"`Giacenza` INT(12), " & _
	"`UltCarico` INT(12), " & _
	"`TotScarico` INT(12), " & _
	"`PrezzoAcq` DECIMAL(12,2), " & _
	"`PrezzoVendita` DECIMAL(12,2), " & _
	"`IdUtenteBlocco` VARCHAR(6)CHARACTER SET utf8, " & _
	"`Blocco` VARCHAR(3)CHARACTER SET utf8)"
I put the data in the server with :

Code: Select all

FQuery = "INSERT INTO " & FDBPrefix & "personamarket_magazzino (" & _
	"CodMag, CodBarre, Descrizione, Giacenza, UltCarico, TotScarico, " & _
	"PrezzoAcq, PrezzoVendita, Fornitore, IdFornitore" & _
	") VALUES (" & _
	"`811`, `8829912991`, `Box mini ssz`, 11, 20, 9,  `22.99`, `35.99`, `FDExpress`, 11 )"
And when I watch with Phpmyqdmin that work correctly but the problem is in the reading of thi table:

Code: Select all

dim CodiceProd as String
dim Desc as string
dim Gia as Integer
dim ultcar as Integer

	Row = mysql_fetch_row(MySQLOut)

	CodiceProd = *Row[1] 
	Desc = *Row[3] 
	Gia = *Row[4] 
	ultcar = *Row[5] 
	
	Print CodiceProd ' this give: 811
	Print Desc ' this give: Box mini ssz
	Print Gia '***** This give 89 but the value is 11
	Print ultcar '***** this give 92 but the value is 20
	
	
The software give no error but the value INT viewed is not equal to the data in the table , I dont know where I wrong
Last edited by exagonx on Aug 20, 2016 20:55, edited 1 time in total.
Drago
Posts: 116
Joined: Aug 10, 2005 13:15

Re: mysql_fetch_row(MySQLOut)

Post by Drago »

Looking at your Create Statement, gia and ultcar are not row[4] row[5] if you fetch full row from sql Server
should be 6 and 7

Greetings
Rainer
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: mysql_fetch_row(MySQLOut)

Post by exagonx »

Drago wrote:Looking at your Create Statement, gia and ultcar are not row[4] row[5] if you fetch full row from sql Server
should be 6 and 7

Greetings
Rainer

Please help me I fond the problem :

Code: Select all

Dim IdRow as Integer

IdRow = *Row[0]

Print *Row[0] ' give ( 1 )
Print IdRow ' give (50)

How I can convert the MySQL INT to FBC Integer and MySQL Decimal to FBC Double ?
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: mysql_fetch_row(MySQLOut)

Post by caseih »

It looks like the row is returned as a null-terminated array of null-terminated strings. Presumably you could do something like:

Code: Select all

Dim IdRow as Integer

IdRow = cint(*Row[0])
Apologies for the many edits. I first tried INT() which didn't work, but VAL() did, but had an intermediate floating point conversion in there. I finally rediscovered CINT() which does the trick for integers.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: mysql_fetch_row(MySQLOut)

Post by exagonx »

caseih wrote:It looks like the row is returned as a null-terminated array of null-terminated strings. Presumably you could do something like:

Code: Select all

Dim IdRow as Integer

IdRow = cint(*Row[0])
Apologies for the many edits. I first tried INT() which didn't work, but VAL() did, but had an intermediate floating point conversion in there. I finally rediscovered CINT() which does the trick for integers.
Yes work, now I have to find a way for translate the decimal to double meybe is better if I treat like string and then i can use how I need
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: mysql_fetch_row(MySQLOut)

Post by caseih »

If you want to turn a string into double, just use VAL(*row[0]). VAL() parses a string into a double. CINT() parses a string into an integer.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: mysql_fetch_row(MySQLOut)

Post by exagonx »

caseih wrote:If you want to turn a string into double, just use VAL(*row[0]). VAL() parses a string into a double. CINT() parses a string into an integer.
Is not string but DECIMAL type
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: mysql_fetch_row(MySQLOut)

Post by exagonx »

I Tryed

Code: Select all

Dim MyValuea as Double
Dim MyValueb as Double

MyValuea = Cdbl(*Row[9])
MyValueb = Val(*Row[9])

Print *Row[9] ' this give 8.97

Print MyValuea ' this give 8
Print MyValueb ' this give 8

Someone Know how extract the real value from MySQL Array ?
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: mysql_fetch_row(MySQLOut)

Post by caseih »

That's really strange that you're getting those results. VAL() should work as should CDBL(). They both take strings and parse them into doubles. I've tested this several times. Very odd.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: mysql_fetch_row(MySQLOut)

Post by exagonx »

caseih wrote:That's really strange that you're getting those results. VAL() should work as should CDBL(). They both take strings and parse them into doubles. I've tested this several times. Very odd.
I found a solution and seems that works

Code: Select all

Dim MyValueText as String, MyValueDouble as Double

Print *Row[10] ' give 8.97

MyValueText = *Row[10]
MyValueDouble = Val(MyValueText)
Print MyValueDouble 'give  8.97

I have to convert to string and then extract the value with VAL()
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: mysql_fetch_row(MySQLOut)

Post by exagonx »

caseih wrote:That's really strange that you're getting those results. VAL() should work as should CDBL(). They both take strings and parse them into doubles. I've tested this several times. Very odd.
Caseih Thank you for your support , it would take much longer to find a solution without your help.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: mysql_fetch_row(MySQLOut)

Post by dkl »

Row is declared as Zstring Ptr Ptr, isn't it?

Because it's a Zstring Ptr, there can be implicit string indexing when converting to integer, for example:

Code: Select all

var s = "8.97"
dim pz as zstring ptr = strptr(s)

print "string: """ + *pz + """"

dim i as integer = *pz
print "first character ASCII code: " & i & " (""" & chr(i) & """)"
However, when using val() or cdbl() on the expression, such problems shouldn't happen. So with or without intermediate string, it should give the same result. Do you get different results here?

Code: Select all

var s = "8.97"
dim pz as zstring ptr = strptr(s)

print s, val(s), cdbl(s)
print *pz, val(*pz), cdbl(*pz)
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: mysql_fetch_row(MySQLOut)

Post by exagonx »

dkl wrote:Row is declared as Zstring Ptr Ptr, isn't it?

Because it's a Zstring Ptr, there can be implicit string indexing when converting to integer, for example:

Code: Select all

Dim Row As MYSQL_ROW

No Doesnt work

the problem is :

DBMySQL col0=1; Col1="Hello"; Col2=321; Col3=32.1
INT, CHAR, INT, DECIMAL

Dim Col0 as Integer = CInt(*Row[0]) (This work)
Dim Col1 as String = *Row[1] ( This Work )
Dim Col2 as Integer = CInt(*Row[2]) (This Work )
Dim col3 as Double = CDBL(*Row[3]) (This Doesnt work ) give a integer value without a decimal same for VAL

I Have to treat like a string before and then convert in Double

Anyway Is good, because when I store in GTK_LIST_STORE I have to put like string for some reason then I need same colum like string for viewing and like double for calculate with other value.
Post Reply