Jump to content

MySql returns integers as strings - how to return as integers?


Recommended Posts

Hi,

 

I have various tables that contain integer attributes.  When I query any integer value, it is returned as a string - at least in PHP it is of type "string".  Is it possible to maintain the type, so I receive integer values in PHP when querying an integer column?

 

Thanks in advance,

 

Steffen

What exact problem are you having?

 

By definition ALL data that is fetched from a result set is an array of strings, but if the contents of a string happens to be a number, it will be treated as a number by php -

 

Return Values

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

 

The problem ist that I am converting the data to JSON with json_encode, and numbers the difference is:

 

"id":3

vs.

"id":"3"

 

The client reading the JSON doesn't accept the number as a string ("3").

 

And I have a lot of different tables, so it's very cumbersome to go through all columns and to include manual casts in all kinds of places.

 

fbwb

  • 6 months later...

What exact problem are you having?

 

By definition ALL data that is fetched from a result set is an array of strings, but if the contents of a string happens to be a number, it will be treated as a number by php -

 

Return Values

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

 

I always frown upon such replies.  They seem to imply that it should not matter, the question being asked, as long as there is some other way around the problem. These posters usually miss unique situations where a specific answer to the question is required.

The questioner in this case did have a legitimate need, JSON.  I also have one - AJAX.  I prefer to to use === instead of == in my JS coding and the result is different if the types are not matched.

What exact problem are you having?

 

By definition ALL data that is fetched from a result set is an array of strings, but if the contents of a string happens to be a number, it will be treated as a number by php -

 

Return Values

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

 

I always frown upon such replies.  They seem to imply that it should not matter, the question being asked, as long as there is some other way around the problem. These posters usually miss unique situations where a specific answer to the question is required.

The questioner in this case did have a legitimate need, JSON.  I also have one - AJAX.  I prefer to to use === instead of == in my JS coding and the result is different if the types are not matched.

 

Seriously?  You signed up just to post a critique of someone who has been on here helping people for 5 years? and then missed the point completly?  The post starts with the request for the exact issue, what's to frown uppon?  It then procieds to comment to the fact that in most normal cases and in PHP only scenarios the requested process is irrelivent.  This is done regularaly, whither you like it or not, in order to inform people that what they may think is needed coding could actualy just be an excersise in reinventing the wheel.  Again, I fail to see what would be wrong with this, but hey you've got your oppinion and a right to voice it.  Incidently, something I frown uppon is the use of operators chosen by prefference rather than practicality... ::)

@OP

 

Your problem is one of the "MySQL to PHP relationship" w/c I detest. It causes extra work.

 

As a workaround, create a generic function that will "wrap" the need to query the results.

 

In that function, you can use PHP's builtin casting functions or constructs, such as 'settype' or (<type>)$variable, respectively. For the metadata of the table (i.e., what are the respective types of the table's attributes), there are mysql_* functions available for use; please refer to the manual.

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.