Jump to content

Why does SQL return strings to PHP?


kdeff

Recommended Posts

This question comes from an efficienct standpoint.

 

Why does SQL (I've used PGSQL and MySQL) always return variables as strings to PHP?

 

This seems horribly inefficient, as the SQL server will have to transmit bulky string data to PHP when representing anything.  Why pass the string "FALSE" to php when a 1 bit boolean would suffice?

Link to comment
Share on other sites

The reason that SQL returns everything as a string except NULL which since a certain PHP version, I think 5.3, actually returns NULL, is because that it simply can't translate the types correctly.

 

It can't translate a TINYINT, a SMALLINT, a MEDIUMINT, a BIGINT, a DECIMAL, a DATETIME, a YEAR, a TIME, a VARBINARY, a DOUBLE, an ENUM, a SET, ... And these are just the ones that I can think of out of the top of my hat. Not to mention any custom data types in MySQL.

 

So thus since it can only translate about 2 or 3 types correctly it simply doesn't bother and it's expected of the user to do this conversion themselves if desired.

 

Now you may wonder why I included TINYINT, SMALLINT, MEDIUMINT, ENUM, YEAR, etc. This is because you probably don't understand type translation between languages. When the column is a tinyint then in PHP it should be the equivalent not a 32-bit signed integer or you will get problems when you want to update the row (where this translation would also exist, and you can't fit a 32-bit signed integer into a tinyint). The same goes for example a SET, which would be an array where you can only add certain values.

 

SQL: SET('foo', 'bar', 'bat', 'baz')
PHP: $mySet[] = 'world'; // error: world is not in set('foo', 'bar', 'bat', 'baz')
Edited by ignace
Link to comment
Share on other sites

This question comes from an efficienct standpoint.

 

Why does SQL (I've used PGSQL and MySQL) always return variables as strings to PHP?

 

This seems horribly inefficient, as the SQL server will have to transmit bulky string data to PHP when representing anything.  Why pass the string "FALSE" to php when a 1 bit boolean would suffice?

 

It returns 1 for true, not "TRUE"

mysql> SELECT 5=5 as result;
+--------+
| result |
+--------+
|      1 |
+--------+
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.