Jump to content

Mysql variable column name


Recommended Posts

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!

Link to comment
Share on other sites

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 ; =>

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...