Downloads

 

MySQL returns strings…or does it?

I noticed an interesting fact yesterday, which pertains to my original post MySQL returns strings (if you have not read this yet, please do). The PHP function mysql_num_rows(), which returns the number of rows in a result set, I assumed would return the number of rows as a string. Since field values are all returned as strings and for the sake of consistency my initial thought was that this function would also return a string.

One of the first things you should learn as a developer (or human) is to never assume, so I checked the documentation, and to my surprise I was completely wrong, the number actually arrives in PHP as an INT type.

The MySQL C API function mysql_num_rows() returns a my_ulonglong value:

MySQL – Paul DuBois
my_ulonglong – A long integer type, used for the return value of functions that return row counts or other potentially large numbers, such as mysql_affected_rows(), mysql_num_rows(), and mysql_insert_id()…

This information again had me looking through the documentation in the PHP manual and in fact mysql_insert_id() does return an INT type (or FALSE on failure).

I find it interesting that in certain situations the data type coming from the MySQL C API is actually representative of the content of the data and at other times it is not. Not that this gets us any closer to why field values are all returned as strings, but the plot does thicken.

If anyone can shed any light on this topic, or would like to put their own spin on things, please feel free to comment.

In the next couple of days I will write a post explaining how, as a PHP developer, I became interested in variable types, and why you should also.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Improve the web with Nofollow Reciprocity.