php - mysql automatically cast strings to integer -
i've noticed if mysql request one:
select 1 mytable id = 'asdf'
then string 'asdf' casted 0
. means have record id 0
match. format of id
field int(8).
what best way proceed:
- i need check (by php example) value numerical only?
- there mysql way that?
- i must remove record id
0
? (bad)
you must first sanitize inputs via php.
$id = 'asdf'; if(is_numeric($id)){ $query("select 1 mytable id = $id"); }else{ die("id not numeric"); }
or can do:
select 1 mytable id = 'asdf' , 'asdf' regexp '^-?[0-9]+$'
this cause regex = false, causing no rows return.
Comments
Post a Comment