tags:

views:

59

answers:

3

is there any operator equivalent to === in mysql which checks value as well the datatype.

actually i have a u_id field in a MYSQL database which is int type

now from login page where user enter user id and during logicheck i write these code

$loginCode = mysql_real_escape_string($_POST[userCode]);
$sql=SELECT * FROM tbl_user WHERE u_id=".$loginCode ;
$result=$db->query($sql);

this will return a row

now if i enter 2.0 then it also return a row(that i don't want) i want that when 2.0 is written ..mysql does not return any row .

gettype($loginCode) return string.... its strange how a integer type(u_id) field can compare with string type in mysql please suggest me solution or any better way

A: 

You can try convert value from php dynamicly

$loginCode = mysql_real_escape_string($_POST[userCode]);
$sql="SELECT * FROM tbl_user WHERE u_id=".(int)$loginCode ;
$result=$db->query($sql);
antyrat
if i convert into integer then by writing 2.0 to 2.99 will enter ..bcoz `(int)$loginCode` change it into integer ..i want something else...read the question again
diEcho
how about floor((int)$loginCode) ?
antyrat
+1  A: 

you can cast your data and compare them as string :

"SELECT * FROM tbl_user WHERE CAST(u_id as nvarchar(3))=".$loginCode
remi bourgarel
+1 Nice, that would force MySQL to compare strings. For a large table, this could hurt performance, since an index on u_id could not be used. But tbl_user is unlikely to be very large :)
Andomar
it returns following error:`You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'n varchar(3))=2 LIMIT 0, 30' at line 1`
diEcho
Appearantly you can't cast to `varchar`. Try `trim(cast(u_id as nchar(12)))` instead
Andomar
`SELECT * FROM tbl_user WHERE TRIM(CAST(u_id AS nchar(3)))=2.0`Return still return same result
diEcho
It runs without error on my installation of MySQL
Andomar
+1  A: 

All SQL databases will implicitly convert a float to an int.

You could ensure that your login code contains nothing but numbers with a regular expresison. For example:

WHERE u_id = <LoginCode> AND <LoginCode> RLIKE '^[0-9]+$'
Andomar
there in not LoginCode, its `$loginCode` how do i add php varaiable inside <>
diEcho
`$sql="SELECT * FROM tbl_user WHERE u_id=".$loginCode." AND '".$loginCode."' RLIKE '^[0-9]+$'";`
Andomar