Integer in DB becomes string in PHP?

Integer in DB becomes string in PHP?

am 27.08.2007 22:59:22 von srynoname

--0-1002986507-1188248362=:33530
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

hello,

I have a table "test_table" with one column:
number int(11)
(also see phpmyadmin database dump at the end of this mail).
it has two values, 123 and 150.

i select and output one of those numbers using the following code:
--- php code start ---
error_reporting(E_ALL);

$con=mysql_connect('127.0.0.1', 'root', '');
mysql_select_db('testdb', $con);
$result=mysql_query('select * from test_table where number=123');
$row=mysql_fetch_array($result);


print_r($row);

var_dump($row['number']);
?>
--- php code end ---

that outputs:
--- output start ---

Array
(
[0] => 123
[number] => 123
)
string(3) "123"
--- output end ---



1) Is this normal?
2) Can this behaviour be changed / fixed?

Thanks!






---------------------------------
Wissenswertes für Bastler und Hobby Handwerker.BE A BETTER HEIMWERKER!
--0-1002986507-1188248362=:33530--

RE: Integer in DB becomes string in PHP?

am 27.08.2007 23:44:08 von Daevid Vincent

You could use intval($row['number']); to force it, but be careful that
you may hit upper bounds around 2 Billion as integers are always signed
in PHP (unfortunately), and mysql lets you store unsigned. It'll also
depend on your OS.

If you're checking the value only, you can use
is_numeric($row['number']) I think.

And also keep in mind that PHP will cast on the fly as needed...

If ($row['number'] == '123')

And

If ($row['number'] == 123)

Will both return true.

Also, could you change your name from "hjfg sdfdsf" to something more
sane -- unless that really is your name, then I feel sorry for you and
would advise you to go on "Wheel of Fortune", and buy a vowel...

ÐÆ5ÏÐ=20

"Some people, when confronted with a problem, think 'I know, I'll use
XML.'"
Now they have two problems.=20

> -----Original Message-----
> From: hjfg sdfdsf [mailto:stay_reg@yahoo.de]=20
> Sent: Monday, August 27, 2007 1:59 PM
> To: php-db@lists.php.net
> Subject: [PHP-DB] Integer in DB becomes string in PHP?
>=20
> hello,
>=20
> I have a table "test_table" with one column:
> number int(11)
> (also see phpmyadmin database dump at the end of this mail).
> it has two values, 123 and 150.
>=20
> i select and output one of those numbers using the following code:
> --- php code start ---
> > error_reporting(E_ALL);
>=20
> $con=3Dmysql_connect('127.0.0.1', 'root', '');
> mysql_select_db('testdb', $con);
> $result=3Dmysql_query('select * from test_table where number=3D123');
> $row=3Dmysql_fetch_array($result);
>=20
>=20
> print_r($row);
>=20
> var_dump($row['number']);
> ?>
> --- php code end ---
>=20
> that outputs:
> --- output start ---
>=20
> Array
> (
> [0] =3D> 123
> [number] =3D> 123
> )
> string(3) "123"
> --- output end ---
>=20
>=20
>=20
> 1) Is this normal?
> 2) Can this behaviour be changed / fixed?
>=20
> Thanks!
>=20
>=20
>=20
>=20
>=20
> =20
> ---------------------------------
> Wissenswertes für Bastler und Hobby Handwerker.BE A BETTER=20
> HEIMWERKER!
>=20

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php