odbc_result_all formatting - odbc

$selectVolID = "Select COUNT(VolunteerID) from planetVolunteers";
$getVolID = odbc_exec($connect, $selectVolID);
echo odbc_result_all($getVolID);
gives:
Expr1000
49
1
49 is the correct count. I want to change the Expr1000 to something legible and get rid of that 1 (which i assume means there are no more values to count).

You can use an alias in the SELECT statement to a more descriptive name:
SELECT COUNT(VoluneerID) NumVolunteers from ...
Depending on the underlying database engine, it is possible that the AS keyword migh be needed in front of the alias.
I have dealt very little with PHP, so I do not know about the 1 that is printed.

The number at the end should be the total number of rows returned. The best way I know of to remove it would be to put it in a hidden input, which means you can refer to it later with javascript or something if you really needed to, and also lets you hide it easily.. anything wrapped around the odbc_results_all function only effects the returning row count instead of the whole table (so you can use anything, maybe a div will be better).
echo "<input value=\"" . odbc_result_all($getVolID) . "\" type=\"text\" style=\"display:none;\">";

Related

Why is CASE - WHEN - ELSE not working for me in pgadmin in this once instance?

Working with postgres 9.4 and using PGAdmin III: I have a schema with a character field that MAY contain "new lines" (NLs), or MAY contain double spaces, or neither. I need to divide the field into 3 segments by either of the above factors or by 26 characters in length.
If the total length is 26 or less, I just copy it as is.
If it has NLs, then I split it by those.
If no NLs but it has double spaces, I split it by those.
Finally, if none of the above, I want to split it into 3 26 character chunks.
For context; the column is data entered by customers into a 3 line box. anywhere from 4 characters up to 78. Some use NLs, some use spaces to push the text to the next line, some have luck with spacing so use neither.
Here's the part of the query to gather the first line:
CASE WHEN LENGTH(detail) < 27 THEN detail
WHEN detail LIKE E'%\n%' THEN SPLIT_PART(detail,E'\n',1)
WHEN detail LIKE E'% %' THEN SPLIT_PART(detail,E' ',1)
ELSE LEFT(detail, 26)
END AS line1,
When I test this on a sample schema, the first three parts work exactly as I need but the "ELSE" part never does. The LEFT statement workout outside of the CASE statement but not within.
I've tried enclosing it with a SELECT clause, nesting several CASE-WHEN-ELSE statements, and other things to no avail.
Oddly, it DOES work if I take out the "SPLIT_PART" line referring to '% %'.
Short of giving up on the double-space split, is there another solution, or have I just formatted something wrong?
CASE WHEN LENGTH(detail) < 27 THEN detail
WHEN detail LIKE '%\n%' THEN SPLIT_PART(detail,'\n',1)
WHEN position(' ' in detail) > 0 THEN SPLIT_PART(detail,' ',1)
ELSE left(detail,26)
After another bit of time mucking about, I suspected the LIKE E'% %' might be the issue - and it was. Simply changing it to LIKE E' ' allows it to work like I wanted.
Added:
Turns out the best solution is a combination of "position" to determine if the double space exists, and split-part to separate correctly at the location.

How can I dynamically change the where conditions of a for each loop?

I have a table of records that has two logical flags, holdn and holdl. I want to loop through this table with 3 different criteria.
Either flag is TRUE - We want to see everything that is on hold
Flag holdl is TRUE - We only want see items that are on hold for this one reason
Flag holdn is TRUE - We only want to see items that are on hold for this other reason.
I cannot figure out how to dynamically change the for each loop based on this. What I have tried so far is to set the value of a variable based on these conditions and then use the content of the variable as one of the where parameters. This does not work as Progress complains that there is a data mismatch. The variable is a string, the flags are logical, so that does make sense. See sample code below. This is a snippet of the actual code with the the table name changed. The which-hold, order-from, etc variables are defined and set in a different module which calls this one.
DEFINE VARIABLE which-hold# AS CHARACTER FORMAT "x(30)" NO-UNDO.
CASE which-hold:
WHEN "B" THEN which-hold# = "(widget.holdn or widget.holdl)".
WHEN "L" THEN which-hold# = "widget.holdl".
WHEN "N" THEN which-hold# = "widget.holdn".
END CASE.
for each widget where which-hold# and
widget.order-no >= order-from and widget.order-no <= order-thru and
widget.line-no >= line-from and widget.line-no <= line-thru and
widget.joint-no >= joint-from and widget.joint-no <= joint-thru
no-lock:
A bunch of code to make a nice report with the retrieved records...
end.
Self taught Progress programmer here, who has inherited a huge, poorly documented application. Please be gentle.
If you would prefer not to deal with handles a semi-dynamic approach is also possible:
define variable i as integer no-undo.
define query q for customer.
do while true:
update i.
case i:
when 0 then quit.
when 1 then open query q for each customer no-lock where custNum >= 1000.
when 2 then open query q for each customer no-lock where state = "nh".
otherwise open query q for each customer no-lock where name begins "u".
end.
do while true with frame a:
get next q.
if not available customer then leave.
display custNum name state with frame a 10 down.
down with frame a.
end.
close query q.
end.
What you want is actually a dynamic query. I'll get to it at the end, but first I'd like to explain why you won't be able to try and substitute the field name in the which-hold# variable: because the query is evaluated at compile time. And this is what it reads (supposing which-hold# has a value of widget.holdn
FOR EACH widget where "widget-holdn" (...)
And that does not evaluate to TRUE or FALSE. So what, you ask? Well, that is the key here. Every condition needs to evaluate to true or false, so you'd be more in luck if you try
for each widget where (if widget-hold# = 'widget.holdn' then widget.holdn = true else TRUE) (...)
Again, notice the condition will exist if widget-hold# has the value I want, otherwise it doesn't filter on this at all.
So you can just code the way I showed (for each of the conditions you have) and it should work fine.
BUT let me suggest a dynamic query instead.
You need to have:
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
CREATE QUERY hQuery.
hQuery:SET-BUFFERS(BUFFER widget:HANDLE).
hQuery:QUERY-PREPARE('<THIS IS THE CORE>').
hQuery:QUERY-OPEN().
DO WHILE hQuery:GET-NEXT():
A bunch of code to make a nice report with the retrieved records...
END.
So in the core you have a string that corresponds to your for each the way you want it to look. So it should be for example (store this in a variable, or assemble it inside the query prepare, it doesn't matter):
'FOR EACH widget NO-LOCK WHERE ' +
(if which-hold = 'B' then 'widget.holdn = true and widget.holdl = true'
else if which-hold = 'L' then 'widget-holdl = true'
else /* N */ 'widget-holdn = true').
Remember I said your query is evaluated at compile time? Well, just so you know, dynamic queries on the other end are evaluated at run time, so be prepared for errors to pop up only when you run. Another thing I should mention is dynamic queries are slower than static ones, so please evaluate and choose your poison :)
This should be what you need. Please let me know if it's helpful or any questions remain.

select update within sama table oracle sql or pl/sql -

I need to select v_col1, from table_x and that column gives me string that i need to put(update) into same
rowid but into diffrent column(h_col2) in sama table table_x - sorry it seems easy but i am beginner....
tabl_x
rowid V_col1, h_col2 etc .....
1 672637263 GVRT1898
2 384738477 GVRT1876
3 263237863 GVRT1832
like in this example i need to put GVRT1898 (update) instead of 672637263 and i need to
go into every row in this table_x and fix -
like next line would be (rowid2 would be GVRT1876 instead of 384738477 :-)
this table has 40000 lines like this and i need to loop for every rowid
THX for your responce Justin - this is a little more complex,
i have this string in h_col and need to take only GVRTnumber out and put into v_col - but it's
hard becouse GVRTnumber is in various place in column see down here....
"E_ID"=X:"GVRT1878","RCode"=X:"156000","Month"=d:1,"Activate"=d:5,"Disp_Id"=X:"4673498","Tar"=X:"171758021";
2"E_ID"=X:"561001760","RCode"=X:"156000","Month"=d:1,"Activate"=d:5,"Disp_Id"=X:"GVRT1898","Tar"=X:"171758021";
h_col column have this number that i want but in various place like somethimes it's in this 600byte column it's in byte nr 156 - sometimes in 287 but the only unique is "GVRT...." how can i take that string and put it to v_col -
Can you show me how to write such SQL pl/sql ?
regards & thanks
It sounds like you just want
UPDATE tabl_x
SET h_col2 = v_col1
Of course, if you do something like this, that implies that one of the two columns should be dropped or the data model needs to get fixed. Having two copies of the same data in each row is a bad idea from a normalization standpoint if nothing else.

SQLite X'...' notation with column data

I am trying to write a custom report in Spiceworks, which uses SQLite queries. This report will fetch me hard drive serial numbers that are unfortunately stored in a few different ways depending on what version of Windows and WMI were on the machine.
Three common examples (which are enough to get to the actual question) are as follows:
Actual serial number: 5VG95AZF
Hexadecimal string with leading spaces: 2020202057202d44585730354341543934383433
Hexadecimal string with leading zeroes: 3030303030303030313131343330423137454342
The two hex strings are further complicated in that even after they are converted to ASCII representation, each pair of numbers are actually backwards. Here is an example:
3030303030303030313131343330423137454342 evaluates to 00000000111430B17ECB
However, the actual serial number on that hard drive is 1141031BE7BC, without leading zeroes and with the bytes swapped around. According to other questions and answers I have read on this site, this has to do with the "endianness" of the data.
My temporary query so far looks something like this (shortened to only the pertinent section):
SELECT pd.model as HDModel,
CASE
WHEN pd.serial like "30303030%" THEN
cast(('X''' || pd.serial || '''') as TEXT)
WHEN pd.serial like "202020%" THEN
LTRIM(X'2020202057202d44585730354341543934383433')
ELSE
pd.serial
END as HDSerial
The result of that query is something like this:
HDModel HDSerial
----------------- -------------------------------------------
Normal Serial 5VG95AZF
202020% test case W -DXW05CAT94843
303030% test case X'3030303030303030313131343330423137454342'
This shows that the X'....' notation style does convert into the correct (but backwards) result of W -DXW05CAT94843 when given a fully literal number (the 202020% line). However, I need to find a way to do the same thing to the actual data in the column, pd.serial, and I can't find a way.
My initial thought was that if I could build a string representation of the X'...' notation, then perhaps cast() would evaluate it. But as you can see, that just ends up spitting out X'3030303030303030313131343330423137454342' instead of the expected 00000000111430B17ECB. This means the concatenation is working correctly, but I can't find a way to evaluate it as hex the same was as in the manual test case.
I have been googling all morning to see if there is just some syntax I am missing, but the closest I have come is this concatenation using the || operator.
EDIT: Ultimately I just want to be able to have a simple case statement in my query like this:
SELECT pd.model as HDModel,
CASE
WHEN pd.serial like "30303030%" THEN
LTRIM(X'pd.serial')
WHEN pd.serial like "202020%" THEN
LTRIM(X'pd.serial')
ELSE
pd.serial
END as HDSerial
But because pd.serial gets wrapped in single quotes, it is taken as a literal string instead of taken as the data contained in that column. My hope was/is that there is just a character or operator I need to specify, like X'$pd.serial' or something.
END EDIT
If I can get past this first hurdle, my next task will be to try and remove the leading zeroes (the way LTRIM eats the leading spaces) and reverse the bytes, but to be honest, I would be content even if that part isn't possible because it wouldn't be hard to post-process this report in Excel to do that.
If anyone can point me in the right direction I would greatly appreciate it! It would obviously be much easier if I was using PHP or something else to do this processing, but because I am trying to have it be an embedded report in Spiceworks, I have to do this all in a single SQLite query.
X'...' is the binary representation in sqlite. If the values are string, you can just use them as such.
This should be a start:
sqlite> select X'3030303030303030313131343330423137454342';
00000000111430B17ECB
sqlite> select ltrim(X'3030303030303030313131343330423137454342','0');
111430B17ECB
I hope this puts you on the right path.

"Variable variable" syntax

This is a question related to getting Drupal CCK fields (just in case that happens to change anything).
I have several Drupal CCK fields with similar names. They have the same name with a number at the end. that I'd like to pull values from these fields (ten fields total). This is the syntax for accessing the fields values:
$node->cck_field_1[0]['value']
$node->cck_field_2[0]['value']
$node->cck_field_3[0]['value']
…etc.
Since they're all separate fields, but they're numbered, I'd like to just loop through incrementally to write out what I need (there's a lot more to what I'm writing than just accessing these fields' data, but they're the determining factors of the rest), but I can't figure out how to insert a variable into that part of the code.
e.g., (if $i were the incremental number variable), I'd like to be able to write the following string as a variable:
'$node->cck_field_' . $i . '[0]["value"]'
I understand about using the curly brackets to create a variable name from a string, but the part I need the variable in needs to be outside of the string. e.g. this works:
${node}->cck_field_1[0]['value']
but this doesn't:
${node->cck_field_1}[0]['value']
(so I can't write ${'node->cck_field'.$i}[0]['value'] )
So how can write this so that I can use $i in place of the number?
This should work:
$node->{'cck_field_' . $i}[0]['value']

Resources