Jump to content
Sign in to follow this  
EddieAdams

Mysql variable column name

Recommended Posts

EddieAdams

With a MySQL database I want to get a value from a column name. But the column name is a a variable.

CODE
.Fields($columnName).value

This outputs an error:

==> Error in expression.

Any ideaS?

Share this post


Link to post
Share on other sites
ResNullius

With a MySQL database I want to get a value from a column name. But the column name is a a variable.

CODE
.Fields($columnName).value
This outputs an error:

==> Error in expression.

Any ideaS?

What is your surrounding code? Are you trying to store the value in a variable too? MsgBox it? Write it to a file?

Are you using a "With" statement,

$columnName = "LastName"
With $oMySqlDb
  $colValue = .Fields($columnName).value
EndWith

or does the database object reference come in line before this,

$columnName = "LastName"
$colValue = $oMySqlDb.Fields($columnName).value

We need to see more to tell more!

Share this post


Link to post
Share on other sites
EddieAdams

CODE
Func FuncName()

$SQLOBJ = _MySQLConnect($UserName,$Password,$Database,$MySQLServerName)

$SQLCode = "SELECT * FROM tbl_Name'"

$TableContents = _Query($SQLOBJ,$SQLCode)

$columnName = "columnName"

$NameList = ""

With $TableContents

While Not .EOF

$NameList &= $SQLCode & ", " & .Fields("cards").value & ", " & .Fields($columnName).value & @CRLF

.MoveNext

WEnd

EndWith

_MySQLEnd($SQLOBJ)

ConsoleWrite($NameList & @LF) ; debug info

EndIf

Next

EndFunc ; =>

Share this post


Link to post
Share on other sites
ResNullius

$columnName = "columnName"
Is the column name in the table literally "columnName" ???

If so, I will dig a little deeper (and whoever designed the table should be shot!)

If not, replace it with the actual name of the column.

Also, are you very experienced with SQL , ADO, and the like?

Or a bit of a noob?

Just need to know so I don't take make certain assumptions one way or the other :P

Share this post


Link to post
Share on other sites
EddieAdams

No, I changed it to columnname for this example. Noob nor expert.

Edited by EddieAdams

Share this post


Link to post
Share on other sites
EddieAdams

The original script doesnt have a preserved column name.

Share this post


Link to post
Share on other sites
ResNullius

No, I changed it to columnname for this example. Noob nor expert.

OK, first try this

Func FuncName()
$SQLOBJ = _MySQLConnect($UserName,$Password,$Database,$MySQLServerName)

$SQLCode = "SELECT * FROM tbl_Name'"
$TableContents = _Query($SQLOBJ,$SQLCode)
$columnName = "columnName"

$NameList = ""
With $TableContents
  While Not .EOF
    $colValue = .Fields($columnName).value
    ConsoleWrite($colValue & @CRLF)
    .MoveNext
  WEnd
EndWith
_MySQLEnd($SQLOBJ)

EndFunc; =>

See if that outputs the values as expected.

Note: in you orginal function as posted above, you have an orphaned EndIf and an orphaned Next statement. Don't know if they're there in your production code, but they should be removed.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.