Jump to content

[SOLVED] MySQL return issue


Sephiriz

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.