Sephiriz Posted March 26, 2007 Share Posted March 26, 2007 I've coded for a while, but have been on a nearly 2 year hiatus, so I'm currently refreshing my memory by trying to code simple things. I have a unit table, and I have an owner_id column, which is an int data type, unsigned. In a test row I made, I set owner_id to 1. I send the appropriate query to the database, and I then get the result, which SHOULD be an integer, but for some reason, I keep on getting a number 1 in a string variable. Why won't I get an integer result if the data type is integer? $query = "SELECT * from units"; $result = $mysqli->query($query); /* numeric array */ while ($row = $result->fetch_array(MYSQLI_ASSOC) ) { //print_r($row); echo gettype($row['owner_id']); } Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/ Share on other sites More sharing options...
hitman6003 Posted March 27, 2007 Share Posted March 27, 2007 The type doesn't really matter in PHP since it's dynamically typed. You can force it to typecast if you like, but there's no reason to since PHP will convert from string to int, and vice-versa, when necessary. Reference Type Juggling in the php manual...here's an english link: http://us2.php.net/manual/en/language.types.type-juggling.php Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-215781 Share on other sites More sharing options...
Sephiriz Posted March 27, 2007 Author Share Posted March 27, 2007 I understand that, but at no point did I indicate that it should be anything but an integer. Um, quick question I guess then: can the various parts of an array be different types of variables? Like $test[0] is a string, and $test[1] is an integer? Or is $test an array where all values are strings. Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-215788 Share on other sites More sharing options...
hitman6003 Posted March 27, 2007 Share Posted March 27, 2007 Arrays can contain mixed variable types. You may want to read this page for further information: http://us3.php.net/manual/en/language.types.array.php Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-215801 Share on other sites More sharing options...
Sephiriz Posted March 28, 2007 Author Share Posted March 28, 2007 Alright, but nobody knows why if I pull an integer from a table, that when I use fetch_assoc() it turns the result into a string? Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-216487 Share on other sites More sharing options...
hitman6003 Posted March 28, 2007 Share Posted March 28, 2007 It doesn't matter in the end if it is returned as a string or int...php will convert it to the type that it needs to. Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-216492 Share on other sites More sharing options...
Sephiriz Posted March 28, 2007 Author Share Posted March 28, 2007 My problem is that I want to use the result from that value in a function which makes sure that the value passed through is an integer by using is_int(). That's my problem, and I know there are work-arounds, but I'm genuinely curious as to why I'm getting the result I'm getting, because nobody else has ever said anything about all mysql results being automatically turned into strings, even if the result was not in a VARCHAR field or something. Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-216498 Share on other sites More sharing options...
hitman6003 Posted March 28, 2007 Share Posted March 28, 2007 If you are pulling the value from the database, then you have presumably checked the value previously to insure that it is an integer. If so, just typecast it to an int before passing to your function. You may also want to consider using the is_numeric function instead of is_int.... http://www.php.net/is_numeric Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-216503 Share on other sites More sharing options...
hitman6003 Posted March 28, 2007 Share Posted March 28, 2007 After reading through the manual, it looks like having a decimal point ( . ) in your number will cause it to be read as a numeric string, rather than an integer...from the man page on integers: Integers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based) notation, optionally preceded by a sign (- or +). If you use the octal notation, you must precede the number with a 0 (zero), to use hexadecimal notation precede the number with 0x. http://us3.php.net/manual/en/language.types.integer.php EDIT: here is an example of how php does it's type comparisons: $int = 10; $float = 10.55; $string = "10.90"; $not_a_number = "abcd"; echo "<pre> int $int: is_int: " . is_int($int) . " is_float: " . is_float($int) . " is_string: " . is_string($int) . " is_numeric: " . is_numeric($int) . " float $float: is_int: " . is_int($float) . " is_float: " . is_float($float) . " is_string: " . is_string($float) . " is_numeric: " . is_numeric($float) . " string $string: is_int: " . is_int($string) . " is_float: " . is_float($string) . " is_string: " . is_string($string) . " is_numeric: " . is_numeric($string) . " not_a_number $not_a_number: is_int: " . is_int($not_a_number) . " is_float: " . is_float($not_a_number) . " is_string: " . is_string($not_a_number) . " is_numeric: " . is_numeric($not_a_number); Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-216504 Share on other sites More sharing options...
btherl Posted March 28, 2007 Share Posted March 28, 2007 Seems no-one has answered the OP's question.. The answer is "no". You should not get an integer, even if the column is an integer type. You should get a string, or the null type if the value in the column is null. End of story http://sg.php.net/manual/en/function.mysqli-fetch-array.php "Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset." Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-216522 Share on other sites More sharing options...
hitman6003 Posted March 28, 2007 Share Posted March 28, 2007 I swear I looked in the manual for that exact sentence and didn't see it....I must be going crazy Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-216523 Share on other sites More sharing options...
Sephiriz Posted March 28, 2007 Author Share Posted March 28, 2007 Alright, thanks for everyones help. I suppose that I'll just need to do a typecasting then. Appreciate the help. Link to comment https://forums.phpfreaks.com/topic/44425-solved-mysql-return-issue/#findComment-216688 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.