fbwb Posted February 16, 2011 Share Posted February 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/ Share on other sites More sharing options...
lastkarrde Posted February 16, 2011 Share Posted February 16, 2011 http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting Whammy! Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/#findComment-1174912 Share on other sites More sharing options...
fbwb Posted February 16, 2011 Author Share Posted February 16, 2011 Thanks, but I don't want to cast every column manually, I just want to keep the types set in MySQL. So I want PHP to automatically receive integers when the MySQL column type is set to int. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/#findComment-1174915 Share on other sites More sharing options...
PFMaBiSmAd Posted February 16, 2011 Share Posted February 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/#findComment-1174959 Share on other sites More sharing options...
fbwb Posted February 16, 2011 Author Share Posted February 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/#findComment-1174961 Share on other sites More sharing options...
PFMaBiSmAd Posted February 16, 2011 Share Posted February 16, 2011 <?php $row['id'] = '3'; $row['desc'] = 'text'; function make_int($val){ return is_numeric($val) ? (int)$val : $val ; } $row = array_map('make_int',$row); echo json_encode($row); ?> Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/#findComment-1174970 Share on other sites More sharing options...
dreadrocksean Posted August 22, 2011 Share Posted August 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/#findComment-1260410 Share on other sites More sharing options...
Muddy_Funster Posted August 22, 2011 Share Posted August 22, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/#findComment-1260418 Share on other sites More sharing options...
ebmigue Posted August 23, 2011 Share Posted August 23, 2011 @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. Quote Link to comment https://forums.phpfreaks.com/topic/227849-mysql-returns-integers-as-strings-how-to-return-as-integers/#findComment-1260741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.