#152163 - 2005-11-24 12:37 AM
2d arrays and getting data on multiple lines? (using DBgetrecordset UDF)
|
Tomarse
Fresh Scripter
Registered: 2005-04-26
Posts: 9
|
I'm having some trouble using 2d arrays as created from SQL database recordsets using the DBGetRecordset UDF.
The actual database access isnt causing me a problem but pulling the data out of the array afterwards is. What am i doing wrong?
This UDF is supposed to return me a recordset that is a proper 2D array, but i instead seem to get a 1D array with each item consisting of multiple lines of data.
The code i'm attempting is as follows
Code:
$SQLQueryRS = "SELECT id,mac,name,locationid FROM computers where locationid is NULL" $recordset = DBGetRecordset($objConn, $SQLQueryRS) $retvalue = DBconnclose($objconn)
For $row=0 to Ubound($recordset,1) ? $recordset[$row,2] Next
This gives me the error: Code:
ERROR : expected ']'! Script: M:\bin\sql\setup2.kix Line : 157
Line 157 is ? $recordset[$row,2] - which should be displaying the second column in the current $row.
If i change my code to the following:
Code:
$SQLQueryRS = "SELECT id,mac,name,locationid FROM computers where locationid is NULL" $recordset = DBGetRecordset($objConn, $SQLQueryRS) $retvalue = DBconnclose($objconn)
For $row=0 to Ubound($recordset,1) ? $recordset[$row] Next
I get the following output
Code:
1 0000E886E355 TOM-SSSSS
which has been correctly pulled out of my database and contains a complete row of data but on multiple lines. How do i access the items seperately? (for reference the row in my database contains "1,0000E886E355,TOM-SSSSS,NULL" which is 1 row of data with 4 fields.)
Can anybody tell me what i am doing wrong here?
I get similar behavior and end up with multi-lined strings when i try and pull the complete list of section's out of an ini file using ReadProfileString like this
Code:
ReadProfileString ($machinesini,"","")
Thanks
|
|
Top
|
|
|
|
#152165 - 2005-11-24 07:33 PM
Re: 2d arrays and getting data on multiple lines? (using DBgetrecordset UDF
|
Tomarse
Fresh Scripter
Registered: 2005-04-26
Posts: 9
|
Sorry, I have done that but i didnt include it in my code snippet. I've got
Code:
$objConn = DBConnOpen($dsn)
before the section above.
I know your DB stuff is working for me as i can display the output. Its the format that the recordset comes back in that is causing me trouble.
|
|
Top
|
|
|
|
#152167 - 2005-11-24 08:38 PM
Re: 2d arrays and getting data on multiple lines? (using DBgetrecordset UDF
|
Tomarse
Fresh Scripter
Registered: 2005-04-26
Posts: 9
|
Thanks! I've got it working with DBcommand() now after looking at your example code. I'll use that instead.
This works perfectly for me:
Code:
$dsn='DRIVER=SQL Server;SERVER=192.168.1.4;UID=user;PWD=pass;DATABASE=db' $sql='SELECT id,mac,name FROM computers where locationid is NULL' $recordset = DBCommand($dsn,$sql) ? 'Error = '+@ERROR+' - '+@SERROR
For $row=0 to Ubound($recordset,1) For $column=0 to Ubound($recordset,2) ? 'Field(row='+$row+', column='+$column+') ='+$recordset[$row,$column] Next Next
What is the actual difference between DBcommand and DBgetrecordset?
I've been playing with DBgetrecordset too (for no particular reason) and i cant get that example code to work:
Code:
$objConn = DBConnOpen('DRIVER=SQL Server;SERVER=192.168.1.4;UID=user;PWD=pass;DATABASE=db') $recordset = DBGetRecordset($objConn,"SELECT id,mac,name FROM computers where locationid is NULL") $retcode = DBConnClose($objConn) ? "ubound($recordset,1)="Ubound($recordset,1) ;this is 2 ? "ubound($recordset,2)="Ubound($recordset,2) ;this is -1 ? For $row=0 to Ubound($recordset,1) ?"whole row is" $recordset[$row] For $column=0 to Ubound($recordset,2) ? 'Field ='+$recordset[$row,$column] Next Next
I always get -1 for the second ubound. There are similar threads already about but they dont explain why.
Edited by Tomarse (2005-11-24 08:40 PM)
|
|
Top
|
|
|
|
#152169 - 2005-11-25 07:53 PM
Re: 2d arrays and getting data on multiple lines? (using DBgetrecordset UDF
|
Tomarse
Fresh Scripter
Registered: 2005-04-26
Posts: 9
|
Thanks. That makes sense
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 302 anonymous users online.
|
|
|