Page 1 of 1 1
Topic Options
#138317 - 2005-04-20 11:26 PM Quick Question: Which UDF do I use to query a DB that returns an array?
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
I want to be able to query a database and have the results returned in an array that I can count() and manipulate. Would I use DBGetRecordSet, DBExecuteSQL, ???
Top
#138318 - 2005-04-21 01:41 AM Re: Quick Question: Which UDF do I use to query a DB that returns an array
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
I have done the task with DBCommand().
Code:
  
$SQLQuery = "Select Field from table"
$Array = DBCommand($String,$SQLQuery)
For $i=0 to Ubound($Array)
? $Array[$i]
Next



Hope it helps.
_________________________
Life is fine.

Top
#138319 - 2005-04-21 05:23 PM Re: Quick Question: Which UDF do I use to query a DB that returns an array
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
using this code, I was able to get the results to print to the screen but count() isn't working. Can anyone give me any direction as to what I'm doing wrong?

Code:
$sql				= "SELECT compid FROM computers WHERE Name='$HostName' AND MACAddress='$MAC'"
$retcode = DBGetRecordSet($objConn,$sql)
$datediff = $UnixDate - $UnixMonth

? $retcode
for $i=0 to ubound($retcode,1)
for $x=0 to ubound($retcode,2)
$test = $retcode[$i,$x]
? $test
next
next



Eventually what I want to do is be able to count the results of the query and then perform another database transaction based off of the value of that number. Can anyone provide any direction?

Top
#138320 - 2005-04-21 05:28 PM Re: Quick Question: Which UDF do I use to query a DB that returns an array
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Code:
ubound($retcode,1)



...is the size of the sub-array, and therefore the number of results in the set.

Top
#138321 - 2005-04-21 05:37 PM Re: Quick Question: Which UDF do I use to query a DB that returns an array
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
wouldn't the actual result be the result of that ubound + 1 though since arrays start at 0?

-- Tested this out and it's true, I got a return of 11 and I have 12 records for the SQL query. Thank you Richard.


Edited by thepip3r (2005-04-21 05:40 PM)

Top
#138322 - 2005-04-21 06:30 PM Re: Quick Question: Which UDF do I use to query a DB that returns an array
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
thepip3r:
There are some concepts which you have to be familiared with to understands how to manipulate an array.
Refer to the manual for Dynamic Program Variables.

First:
We Get rid of "$datediff = $UnixDate - $UnixMonth" cause we have to make it work AND then do any calculation.

Second:
"? $retcode" Is NOT gonna return anything cause it is an array and not a string, you are treating it as a variable and not an array.

Kixtart can support multi dimension array (up to 60) that is you think of an Excel sheet every new dimension you have just think it of a new column, so....
In your case you have a 1 demension array result of the fact that you do 'SELECT compid FROM' = single column.
If you where to deal with multidimensional your select should be FE 'SELECT compid, Ip, OS FROM' wich is not the case.

"Ubound($retcode)" is gonna tell you a number (total ammount of rows) wich is the ammount of times that 'For' is gonna Loop or walk the array, like this '$test = $retcode[$x]' in wich $x will increase up to $CountArray by one.
I havent seen any count() function you just get it from the ubound().


Code:

$sql = "SELECT compid FROM computers WHERE Name='$HostName' AND MACAddress='$MAC'"
$retcode = DBGetRecordSet($objConn,$sql)

$CountArray=Ubound($retcode) ;top value number of array rows.
For $i=0 to $CountArray
$row = $retcode[$x]
? "Row="+$row
? "Counter="+$x
Next



Sure there is a more specific terminology but I tried to be understandable on the first place.

Give a shout in case you are stucked.
_________________________
Life is fine.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.157 seconds in which 0.093 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org