Forum Moderators: coopster

Message Too Old, No Replies

MySQL int coming back as a string

I'm sure it's insanely simple but it's driving me nuts

         

formasfunction

5:12 pm on Apr 3, 2008 (gmt 0)

10+ Year Member



I have the following method returning a single value from an integer field in my MySQL DB:

static public function memberLevel($g_id, $u_id){
global $database;
$result = $database->query("SELECT level FROM ".TBL_MEMBERS." WHERE
group_id = ".$g_id." AND
user_id = ".$u_id);
if(mysql_numrows($result) == 0) return false;
else return mysql_result($result,0,0);
}

The problem I'm having is that I need to do a strict comparison against what's returned but for some reason what I'm getting back is a string rather than an integer so:

Class::memberLevel(1, 1) === 4 //this would return false
Class::memberLevel(1,1,) === "4" //this would return true

I need to do a strict compare because if there are no rows returned by the query then the method returns false whereas 0 is a valid member level so I don't want them to evaluate the same. What do I need to do to get a valid integer back from MySQL rather than a string?

cameraman

5:38 pm on Apr 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



string mysql_result [us.php.net] ( resource $result , int $row [, mixed $field ] )

You could either use one of the fetch functions or check for false inside your function:
if(mysql_numrows($result) == 0) return false;
$rtn = mysql_result($result,0,0);
return(($rtn === false) ? false : intval($rtn));

formasfunction

5:59 pm on Apr 3, 2008 (gmt 0)

10+ Year Member



Thanks, I was having a lapse in judgment and I'm not sure why this didn't occur to me sooner.

formasfunction

6:15 pm on Apr 3, 2008 (gmt 0)

10+ Year Member



Hmmm, using mysql_fetch_row and mysql_fetch_array didn't work for me, I ended up using:

intval(mysql_result($result,0,0));

Any other reason I might be having problems? I've double checked the mysql field just to make sure it's set to "int".

cameraman

7:52 pm on Apr 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I ran some tests and sure enough had the same results as you. The table I was testing was a MyISAM type. Then I checked it on an InnoDB table and those integers test as true integers.

So if it's really important (since you have the 'workaround' working it may not be much of an issue now) you'll probably want to test different table types to see what's what (lol or I suppose it may just be documented somewhere ;) ).

coopster

8:06 pm on Apr 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to look more closely at the function you are using. Note the very first word in the second message of this thread that shows the definition for the mysql_result function. The return value is of type string.

mysql_fetch_array [php.net] and other fetch operations do the same thing ...


Return Values

Returns an array of strings that corresponds to the fetched row, ...

my emphasis added

In order to get an integer value you either have to cast it as such like you did or use it in an operation that requires PHP to convert it to a numeric value.

cameraman

8:25 pm on Apr 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, I was even looking for the word string in the fetch's but didn't see it.

Now I see what I did - sometime before the grand idea to test on a different table type I'd changed from is_int() to is_numeric() just to double-check that I wasn't daffy, and I forgot to change it back (I'm using a script that I keep reusing for WW posts/tests - I stuck the fetch at the top but the is_int() & echo are 40 lines down).

coopster

4:16 pm on Apr 4, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry cameraman! My response was not directed at you but to the question in formasfunction's first post ...

What do I need to do to get a valid integer back from MySQL rather than a string?

However, I was wondering why you were showing an INTEGER type there ;)

Thanks for the update!

formasfunction

10:06 pm on Apr 6, 2008 (gmt 0)

10+ Year Member



I appreciate the clarification from both of you guys. I'm surprised there's not more interplay between PHP and MySQL in terms of data types but PHP seems to be pretty loose about them in general.