473,770 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pg_fetch_result () always returns strings

Hi.

The manual for pg_fetch_result () reads:

| [..] All forms of integer types are returned as integer values. All
| forms of float, and real types are returned as float values. Boolean
| is returned as "t" or "f". All other types, including arrays are
| returned as strings formatted in the same default PostgreSQL manner
| that you would see in the psql program.

This is not what happens when I call pg_fetch_result ():

pg_query($conn, "create table foo (id int)");
pg_query($conn, "insert into foo values (42)");
$result = pg_query($conn, "select * from foo");
$id = pg_fetch_result ($result, 0, 0);
var_dump($id);

// prints: string(2) "42"

I get the same behaviour for other types of integers, and for floats.
Booleans are returned as "t" and "f" (quite unfortunately, as both
strings evaluate to true in PHP), but that's at least what the manual
says should happen.

Is there any way to get PHP to do the casting for me, or do I really
have to convert all the values manually? I'm currently using PHP 4.3.1
with PostgreSQL 7.3.2.

TIA,
stefan
Jul 17 '05 #1
3 3151
ljb
sp************* ********@ausgeh aucht.sensenman n.at wrote:
Hi.

The manual for pg_fetch_result () reads:

| [..] All forms of integer types are returned as integer values. All
| forms of float, and real types are returned as float values. Boolean
| is returned as "t" or "f". All other types, including arrays are
| returned as strings formatted in the same default PostgreSQL manner
| that you would see in the psql program.

This is not what happens when I call pg_fetch_result ():

pg_query($conn, "create table foo (id int)");
pg_query($conn, "insert into foo values (42)");
$result = pg_query($conn, "select * from foo");
$id = pg_fetch_result ($result, 0, 0);
var_dump($id);

// prints: string(2) "42"

I get the same behaviour for other types of integers, and for floats.
Booleans are returned as "t" and "f" (quite unfortunately, as both
strings evaluate to true in PHP), but that's at least what the manual
says should happen.

Is there any way to get PHP to do the casting for me, or do I really
have to convert all the values manually? I'm currently using PHP 4.3.1
with PostgreSQL 7.3.2.


You are right - it doesn't work as documented. PHP (through 4.3.3 at least)
has no code to convert results to specific types, but returns everything as
strings (not counting the new "experiment al" functions). Feel free to
submit a bug report, but I would make it a documentation bug, since I think
that PHP should not try to do the conversion. It would be a lot less
efficient than you converting it in your PHP script. The PostgreSQL
interface library libpq hands everthing back to PHP as strings anyway, so
PHP would have to find the types (by OID, then ask the database to map that
to a type name), then do the conversion. Since your script 'knows' what
type is expected, it's got to be more efficient to do any needed conversion
in your PHP script.
Jul 17 '05 #2
Stefan Weiss <sp************ *********@ausge haucht.sensenma nn.at> wrote in message news:<36******* *********@weyou n.foo.at>...
Hi.

The manual for pg_fetch_result () reads:

| [..] All forms of integer types are returned as integer values. All
| forms of float, and real types are returned as float values. Boolean
| is returned as "t" or "f". All other types, including arrays are
| returned as strings formatted in the same default PostgreSQL manner
| that you would see in the psql program.

This is not what happens when I call pg_fetch_result ():

pg_query($conn, "create table foo (id int)");
pg_query($conn, "insert into foo values (42)");
$result = pg_query($conn, "select * from foo");
$id = pg_fetch_result ($result, 0, 0);
var_dump($id);

// prints: string(2) "42"

I get the same behaviour for other types of integers, and for floats.
Booleans are returned as "t" and "f" (quite unfortunately, as both
strings evaluate to true in PHP), but that's at least what the manual
says should happen.

Is there any way to get PHP to do the casting for me, or do I really
have to convert all the values manually? I'm currently using PHP 4.3.1
with PostgreSQL 7.3.2.

TIA,
stefan


The way to get PHP to do the casting for you is to ignore type,
and let type be inferred from the operation being performed.
Jul 17 '05 #3
ljb <lj****@mindspr ing.nospam.com> wrote:
Feel free to submit a bug report, but I would make it a documentation
bug, since I think that PHP should not try to do the conversion.
I don't agree; at the very least I would like an option to have the
fields converted automatically. That way I would always get a float
out of a float field, regardless of locale settings: if PostgreSQL's
LC_NUMERIC was set to de_DE, the decimal seperator would be a comma,
and PHP would interpret the string "4,321" as 4 (integer).

[@martin: This is why I can't just let PHP do the casting; especially
not in a financial application. ]
It would be a lot less efficient than you converting it in your PHP
script. The PostgreSQL interface library libpq hands everthing back
to PHP as strings anyway, so PHP would have to find the types (by OID,
then ask the database to map that to a type name), then do the conversion.
Since your script 'knows' what type is expected, it's got to be more
efficient to do any needed conversion in your PHP script.


I usually have custom functions that handle all the db access. These
functions don't know in advance what types they are dealing with, so
they would have to call pg_field_type() once for each returned column,
and then convert as needed. That most certainly is not as efficient as
PHP getting the column types while it's talking to the db backend (OIDs
have nothing to do with this, btw).

And even if I knew which types are going to be returned, I would have
to do something like this after each query:

$values = some_query(); // returns 2dim array of rows/fields

for ($i = 0; $i < count($values); ++$i) {

// numfield should be integer value
if ($values[$i]["numfield"] !== null) {
$values[$i]["numfield"] = (int) $values[$i]["numfield"];
}

// boolfield is a boolean field
if ($values[$i]["boolfield"] !== null) {
$values[$i]["boolfield"] = $values[$i]["boolfield"] != "f";
}

// fltfield is a float field
if ($values[$i]["fltfield"] !== null) {
$values[$i]["fltfield"] = parse_float_rep ($values[$i]["fltfield"]);
}

// ... etc ...

}

Not exactly my idea of efficience.

Oh well, we can always upgrade the hardware :-/.
cheers,
stefan
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
8382
by: Bob Smith | last post by:
question in subject. does the standard say anything about this or is it platform dependent? any advice much appeciated /B
5
2773
by: Chad Paquette | last post by:
Hi, We have a legacy cgi app that's written in C. We are encountering an error when we try to retrieve a cgi environment variable. The variable we are getting contains a Base64 encoded distinguished name. If the distinguished is greater than 57 characters in length, a carriage return is appended to the 58th character. This of course causes the Base64 decoder to bomb because a carriage return is not a valid Base64 character. We are...
10
4590
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a zero-length string (always returns a zero-length string when used in a query expression)" **** How many records are there in FirstTable in which Product Is Null. SELECT COUNT(*) AS CountofNullProdcut
4
3343
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias "CM_Get_Device_ID_List_SizeA" (ByRef pulLen As Integer, ByVal pszFilter As Integer, ByVal UlFlags As Integer) As Integer - To get the length of the device list. This seems to work as I get a CR_SUCCESS (I get a number around 8500. But as I'm not sure what is in the
6
11770
by: anonieko | last post by:
string.IsNullOrEmpty is a very flexible function but watch out sometimes you really mean to test not just for 'empty' strings but also 'blank strings'. In other words, if you use string.IsNullOrEmpty on string containing just spaces it returns false. Which is a correct behaviour but not you actually meant/want in your program. Bugs can creep in without testing properly.
12
4723
by: arnuld | last post by:
#include <stdio.h> int main() { printf("%d\n", getchar() != EOF); return 0; } when i run this programme, no matter what i give it as input, output
2
6308
by: MDANH2002 | last post by:
Hi From VB.NET I want to simulate the POST request of the following HTML form <html> <title>HTTP Post Testing</title> <body> <form action=http://www.example.com/postdata enctype="multipart/form-data" method="post" name="testform">
6
3884
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is definined as taking a char * as an argument? For example the C++ header says SDCERR GetCurrentConfig(DWORD *num, char *name); I am using Uint for the *num parameter, which returns the correct value but for *name, I always get back a string of 6 squares....
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.